decode.go (68728B)
1 // Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved. 2 // Use of this source code is governed by a MIT license found in the LICENSE file. 3 4 package codec 5 6 import ( 7 "encoding" 8 "errors" 9 "io" 10 "math" 11 "reflect" 12 "strconv" 13 "time" 14 ) 15 16 const msgBadDesc = "unrecognized descriptor byte" 17 18 const ( 19 decDefMaxDepth = 1024 // maximum depth 20 decDefChanCap = 64 // should be large, as cap cannot be expanded 21 decScratchByteArrayLen = (8 + 2 + 2 + 1) * 8 // around cacheLineSize ie ~64, depending on Decoder size 22 23 // MARKER: massage decScratchByteArrayLen to ensure xxxDecDriver structs fit within cacheLine*N 24 25 // decFailNonEmptyIntf configures whether we error 26 // when decoding naked into a non-empty interface. 27 // 28 // Typically, we cannot decode non-nil stream value into 29 // nil interface with methods (e.g. io.Reader). 30 // However, in some scenarios, this should be allowed: 31 // - MapType 32 // - SliceType 33 // - Extensions 34 // 35 // Consequently, we should relax this. Put it behind a const flag for now. 36 decFailNonEmptyIntf = false 37 38 // decUseTransient says that we should not use the transient optimization. 39 // 40 // There's potential for GC corruption or memory overwrites if transient isn't 41 // used carefully, so this flag helps turn it off quickly if needed. 42 // 43 // Use it everywhere needed so we can completely remove unused code blocks. 44 decUseTransient = true 45 ) 46 47 var ( 48 errNeedMapOrArrayDecodeToStruct = errors.New("only encoded map or array can decode into struct") 49 errCannotDecodeIntoNil = errors.New("cannot decode into nil") 50 51 errExpandSliceCannotChange = errors.New("expand slice: cannot change") 52 53 errDecoderNotInitialized = errors.New("Decoder not initialized") 54 55 errDecUnreadByteNothingToRead = errors.New("cannot unread - nothing has been read") 56 errDecUnreadByteLastByteNotRead = errors.New("cannot unread - last byte has not been read") 57 errDecUnreadByteUnknown = errors.New("cannot unread - reason unknown") 58 errMaxDepthExceeded = errors.New("maximum decoding depth exceeded") 59 ) 60 61 // decByteState tracks where the []byte returned by the last call 62 // to DecodeBytes or DecodeStringAsByte came from 63 type decByteState uint8 64 65 const ( 66 decByteStateNone decByteState = iota 67 decByteStateZerocopy // view into []byte that we are decoding from 68 decByteStateReuseBuf // view into transient buffer used internally by decDriver 69 // decByteStateNewAlloc 70 ) 71 72 type decNotDecodeableReason uint8 73 74 const ( 75 decNotDecodeableReasonUnknown decNotDecodeableReason = iota 76 decNotDecodeableReasonBadKind 77 decNotDecodeableReasonNonAddrValue 78 decNotDecodeableReasonNilReference 79 ) 80 81 type decDriver interface { 82 // this will check if the next token is a break. 83 CheckBreak() bool 84 85 // TryNil tries to decode as nil. 86 // If a nil is in the stream, it consumes it and returns true. 87 // 88 // Note: if TryNil returns true, that must be handled. 89 TryNil() bool 90 91 // ContainerType returns one of: Bytes, String, Nil, Slice or Map. 92 // 93 // Return unSet if not known. 94 // 95 // Note: Implementations MUST fully consume sentinel container types, specifically Nil. 96 ContainerType() (vt valueType) 97 98 // DecodeNaked will decode primitives (number, bool, string, []byte) and RawExt. 99 // For maps and arrays, it will not do the decoding in-band, but will signal 100 // the decoder, so that is done later, by setting the fauxUnion.valueType field. 101 // 102 // Note: Numbers are decoded as int64, uint64, float64 only (no smaller sized number types). 103 // for extensions, DecodeNaked must read the tag and the []byte if it exists. 104 // if the []byte is not read, then kInterfaceNaked will treat it as a Handle 105 // that stores the subsequent value in-band, and complete reading the RawExt. 106 // 107 // extensions should also use readx to decode them, for efficiency. 108 // kInterface will extract the detached byte slice if it has to pass it outside its realm. 109 DecodeNaked() 110 111 DecodeInt64() (i int64) 112 DecodeUint64() (ui uint64) 113 114 DecodeFloat64() (f float64) 115 DecodeBool() (b bool) 116 117 // DecodeStringAsBytes returns the bytes representing a string. 118 // It will return a view into scratch buffer or input []byte (if applicable). 119 // 120 // Note: This can also decode symbols, if supported. 121 // 122 // Users should consume it right away and not store it for later use. 123 DecodeStringAsBytes() (v []byte) 124 125 // DecodeBytes returns the bytes representing a binary value. 126 // It will return a view into scratch buffer or input []byte (if applicable). 127 // 128 // All implementations must honor the contract below: 129 // if ZeroCopy and applicable, return a view into input []byte we are decoding from 130 // else if in == nil, return a view into scratch buffer 131 // else append decoded value to in[:0] and return that 132 // (this can be simulated by passing []byte{} as in parameter) 133 // 134 // Implementations must also update Decoder.decByteState on each call to 135 // DecodeBytes or DecodeStringAsBytes. Some callers may check that and work appropriately. 136 // 137 // Note: DecodeBytes may decode past the length of the passed byte slice, up to the cap. 138 // Consequently, it is ok to pass a zero-len slice to DecodeBytes, as the returned 139 // byte slice will have the appropriate length. 140 DecodeBytes(in []byte) (out []byte) 141 // DecodeBytes(bs []byte, isstring, zerocopy bool) (bsOut []byte) 142 143 // DecodeExt will decode into a *RawExt or into an extension. 144 DecodeExt(v interface{}, basetype reflect.Type, xtag uint64, ext Ext) 145 // decodeExt(verifyTag bool, tag byte) (xtag byte, xbs []byte) 146 147 DecodeTime() (t time.Time) 148 149 // ReadArrayStart will return the length of the array. 150 // If the format doesn't prefix the length, it returns containerLenUnknown. 151 // If the expected array was a nil in the stream, it returns containerLenNil. 152 ReadArrayStart() int 153 154 // ReadMapStart will return the length of the array. 155 // If the format doesn't prefix the length, it returns containerLenUnknown. 156 // If the expected array was a nil in the stream, it returns containerLenNil. 157 ReadMapStart() int 158 159 reset() 160 161 // atEndOfDecode() 162 163 // nextValueBytes will return the bytes representing the next value in the stream. 164 // 165 // if start is nil, then treat it as a request to discard the next set of bytes, 166 // and the return response does not matter. 167 // Typically, this means that the returned []byte is nil/empty/undefined. 168 // 169 // Optimize for decoding from a []byte, where the nextValueBytes will just be a sub-slice 170 // of the input slice. Callers that need to use this to not be a view into the input bytes 171 // should handle it appropriately. 172 nextValueBytes(start []byte) []byte 173 174 // descBd will describe the token descriptor that signifies what type was decoded 175 descBd() string 176 177 decoder() *Decoder 178 179 driverStateManager 180 decNegintPosintFloatNumber 181 } 182 183 type decDriverContainerTracker interface { 184 ReadArrayElem() 185 ReadMapElemKey() 186 ReadMapElemValue() 187 ReadArrayEnd() 188 ReadMapEnd() 189 } 190 191 type decNegintPosintFloatNumber interface { 192 decInteger() (ui uint64, neg, ok bool) 193 decFloat() (f float64, ok bool) 194 } 195 196 type decDriverNoopNumberHelper struct{} 197 198 func (x decDriverNoopNumberHelper) decInteger() (ui uint64, neg, ok bool) { 199 panic("decInteger unsupported") 200 } 201 func (x decDriverNoopNumberHelper) decFloat() (f float64, ok bool) { panic("decFloat unsupported") } 202 203 type decDriverNoopContainerReader struct{} 204 205 // func (x decDriverNoopContainerReader) ReadArrayStart() (v int) { panic("ReadArrayStart unsupported") } 206 // func (x decDriverNoopContainerReader) ReadMapStart() (v int) { panic("ReadMapStart unsupported") } 207 func (x decDriverNoopContainerReader) ReadArrayEnd() {} 208 func (x decDriverNoopContainerReader) ReadMapEnd() {} 209 func (x decDriverNoopContainerReader) CheckBreak() (v bool) { return } 210 211 // DecodeOptions captures configuration options during decode. 212 type DecodeOptions struct { 213 // MapType specifies type to use during schema-less decoding of a map in the stream. 214 // If nil (unset), we default to map[string]interface{} iff json handle and MapKeyAsString=true, 215 // else map[interface{}]interface{}. 216 MapType reflect.Type 217 218 // SliceType specifies type to use during schema-less decoding of an array in the stream. 219 // If nil (unset), we default to []interface{} for all formats. 220 SliceType reflect.Type 221 222 // MaxInitLen defines the maxinum initial length that we "make" a collection 223 // (string, slice, map, chan). If 0 or negative, we default to a sensible value 224 // based on the size of an element in the collection. 225 // 226 // For example, when decoding, a stream may say that it has 2^64 elements. 227 // We should not auto-matically provision a slice of that size, to prevent Out-Of-Memory crash. 228 // Instead, we provision up to MaxInitLen, fill that up, and start appending after that. 229 MaxInitLen int 230 231 // ReaderBufferSize is the size of the buffer used when reading. 232 // 233 // if > 0, we use a smart buffer internally for performance purposes. 234 ReaderBufferSize int 235 236 // MaxDepth defines the maximum depth when decoding nested 237 // maps and slices. If 0 or negative, we default to a suitably large number (currently 1024). 238 MaxDepth int16 239 240 // If ErrorIfNoField, return an error when decoding a map 241 // from a codec stream into a struct, and no matching struct field is found. 242 ErrorIfNoField bool 243 244 // If ErrorIfNoArrayExpand, return an error when decoding a slice/array that cannot be expanded. 245 // For example, the stream contains an array of 8 items, but you are decoding into a [4]T array, 246 // or you are decoding into a slice of length 4 which is non-addressable (and so cannot be set). 247 ErrorIfNoArrayExpand bool 248 249 // If SignedInteger, use the int64 during schema-less decoding of unsigned values (not uint64). 250 SignedInteger bool 251 252 // MapValueReset controls how we decode into a map value. 253 // 254 // By default, we MAY retrieve the mapping for a key, and then decode into that. 255 // However, especially with big maps, that retrieval may be expensive and unnecessary 256 // if the stream already contains all that is necessary to recreate the value. 257 // 258 // If true, we will never retrieve the previous mapping, 259 // but rather decode into a new value and set that in the map. 260 // 261 // If false, we will retrieve the previous mapping if necessary e.g. 262 // the previous mapping is a pointer, or is a struct or array with pre-set state, 263 // or is an interface. 264 MapValueReset bool 265 266 // SliceElementReset: on decoding a slice, reset the element to a zero value first. 267 // 268 // concern: if the slice already contained some garbage, we will decode into that garbage. 269 SliceElementReset bool 270 271 // InterfaceReset controls how we decode into an interface. 272 // 273 // By default, when we see a field that is an interface{...}, 274 // or a map with interface{...} value, we will attempt decoding into the 275 // "contained" value. 276 // 277 // However, this prevents us from reading a string into an interface{} 278 // that formerly contained a number. 279 // 280 // If true, we will decode into a new "blank" value, and set that in the interface. 281 // If false, we will decode into whatever is contained in the interface. 282 InterfaceReset bool 283 284 // InternString controls interning of strings during decoding. 285 // 286 // Some handles, e.g. json, typically will read map keys as strings. 287 // If the set of keys are finite, it may help reduce allocation to 288 // look them up from a map (than to allocate them afresh). 289 // 290 // Note: Handles will be smart when using the intern functionality. 291 // Every string should not be interned. 292 // An excellent use-case for interning is struct field names, 293 // or map keys where key type is string. 294 InternString bool 295 296 // PreferArrayOverSlice controls whether to decode to an array or a slice. 297 // 298 // This only impacts decoding into a nil interface{}. 299 // 300 // Consequently, it has no effect on codecgen. 301 // 302 // *Note*: This only applies if using go1.5 and above, 303 // as it requires reflect.ArrayOf support which was absent before go1.5. 304 PreferArrayOverSlice bool 305 306 // DeleteOnNilMapValue controls how to decode a nil value in the stream. 307 // 308 // If true, we will delete the mapping of the key. 309 // Else, just set the mapping to the zero value of the type. 310 // 311 // Deprecated: This does NOTHING and is left behind for compiling compatibility. 312 // This change is necessitated because 'nil' in a stream now consistently 313 // means the zero value (ie reset the value to its zero state). 314 DeleteOnNilMapValue bool 315 316 // RawToString controls how raw bytes in a stream are decoded into a nil interface{}. 317 // By default, they are decoded as []byte, but can be decoded as string (if configured). 318 RawToString bool 319 320 // ZeroCopy controls whether decoded values of []byte or string type 321 // point into the input []byte parameter passed to a NewDecoderBytes/ResetBytes(...) call. 322 // 323 // To illustrate, if ZeroCopy and decoding from a []byte (not io.Writer), 324 // then a []byte or string in the output result may just be a slice of (point into) 325 // the input bytes. 326 // 327 // This optimization prevents unnecessary copying. 328 // 329 // However, it is made optional, as the caller MUST ensure that the input parameter []byte is 330 // not modified after the Decode() happens, as any changes are mirrored in the decoded result. 331 ZeroCopy bool 332 333 // PreferPointerForStructOrArray controls whether a struct or array 334 // is stored in a nil interface{}, or a pointer to it. 335 // 336 // This mostly impacts when we decode registered extensions. 337 PreferPointerForStructOrArray bool 338 339 // ValidateUnicode controls will cause decoding to fail if an expected unicode 340 // string is well-formed but include invalid codepoints. 341 // 342 // This could have a performance impact. 343 ValidateUnicode bool 344 } 345 346 // ---------------------------------------- 347 348 func (d *Decoder) rawExt(f *codecFnInfo, rv reflect.Value) { 349 d.d.DecodeExt(rv2i(rv), f.ti.rt, 0, nil) 350 } 351 352 func (d *Decoder) ext(f *codecFnInfo, rv reflect.Value) { 353 d.d.DecodeExt(rv2i(rv), f.ti.rt, f.xfTag, f.xfFn) 354 } 355 356 func (d *Decoder) selferUnmarshal(f *codecFnInfo, rv reflect.Value) { 357 rv2i(rv).(Selfer).CodecDecodeSelf(d) 358 } 359 360 func (d *Decoder) binaryUnmarshal(f *codecFnInfo, rv reflect.Value) { 361 bm := rv2i(rv).(encoding.BinaryUnmarshaler) 362 xbs := d.d.DecodeBytes(nil) 363 fnerr := bm.UnmarshalBinary(xbs) 364 d.onerror(fnerr) 365 } 366 367 func (d *Decoder) textUnmarshal(f *codecFnInfo, rv reflect.Value) { 368 tm := rv2i(rv).(encoding.TextUnmarshaler) 369 fnerr := tm.UnmarshalText(d.d.DecodeStringAsBytes()) 370 d.onerror(fnerr) 371 } 372 373 func (d *Decoder) jsonUnmarshal(f *codecFnInfo, rv reflect.Value) { 374 d.jsonUnmarshalV(rv2i(rv).(jsonUnmarshaler)) 375 } 376 377 func (d *Decoder) jsonUnmarshalV(tm jsonUnmarshaler) { 378 // grab the bytes to be read, as UnmarshalJSON needs the full JSON so as to unmarshal it itself. 379 var bs0 = []byte{} 380 if !d.bytes { 381 bs0 = d.blist.get(256) 382 } 383 bs := d.d.nextValueBytes(bs0) 384 fnerr := tm.UnmarshalJSON(bs) 385 if !d.bytes { 386 d.blist.put(bs) 387 if !byteSliceSameData(bs0, bs) { 388 d.blist.put(bs0) 389 } 390 } 391 d.onerror(fnerr) 392 } 393 394 func (d *Decoder) kErr(f *codecFnInfo, rv reflect.Value) { 395 d.errorf("no decoding function defined for kind %v", rv.Kind()) 396 } 397 398 func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) { 399 rvSetBytes(rv, d.rawBytes()) 400 } 401 402 func (d *Decoder) kString(f *codecFnInfo, rv reflect.Value) { 403 rvSetString(rv, d.stringZC(d.d.DecodeStringAsBytes())) 404 } 405 406 func (d *Decoder) kBool(f *codecFnInfo, rv reflect.Value) { 407 rvSetBool(rv, d.d.DecodeBool()) 408 } 409 410 func (d *Decoder) kTime(f *codecFnInfo, rv reflect.Value) { 411 rvSetTime(rv, d.d.DecodeTime()) 412 } 413 414 func (d *Decoder) kFloat32(f *codecFnInfo, rv reflect.Value) { 415 rvSetFloat32(rv, d.decodeFloat32()) 416 } 417 418 func (d *Decoder) kFloat64(f *codecFnInfo, rv reflect.Value) { 419 rvSetFloat64(rv, d.d.DecodeFloat64()) 420 } 421 422 func (d *Decoder) kComplex64(f *codecFnInfo, rv reflect.Value) { 423 rvSetComplex64(rv, complex(d.decodeFloat32(), 0)) 424 } 425 426 func (d *Decoder) kComplex128(f *codecFnInfo, rv reflect.Value) { 427 rvSetComplex128(rv, complex(d.d.DecodeFloat64(), 0)) 428 } 429 430 func (d *Decoder) kInt(f *codecFnInfo, rv reflect.Value) { 431 rvSetInt(rv, int(chkOvf.IntV(d.d.DecodeInt64(), intBitsize))) 432 } 433 434 func (d *Decoder) kInt8(f *codecFnInfo, rv reflect.Value) { 435 rvSetInt8(rv, int8(chkOvf.IntV(d.d.DecodeInt64(), 8))) 436 } 437 438 func (d *Decoder) kInt16(f *codecFnInfo, rv reflect.Value) { 439 rvSetInt16(rv, int16(chkOvf.IntV(d.d.DecodeInt64(), 16))) 440 } 441 442 func (d *Decoder) kInt32(f *codecFnInfo, rv reflect.Value) { 443 rvSetInt32(rv, int32(chkOvf.IntV(d.d.DecodeInt64(), 32))) 444 } 445 446 func (d *Decoder) kInt64(f *codecFnInfo, rv reflect.Value) { 447 rvSetInt64(rv, d.d.DecodeInt64()) 448 } 449 450 func (d *Decoder) kUint(f *codecFnInfo, rv reflect.Value) { 451 rvSetUint(rv, uint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))) 452 } 453 454 func (d *Decoder) kUintptr(f *codecFnInfo, rv reflect.Value) { 455 rvSetUintptr(rv, uintptr(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))) 456 } 457 458 func (d *Decoder) kUint8(f *codecFnInfo, rv reflect.Value) { 459 rvSetUint8(rv, uint8(chkOvf.UintV(d.d.DecodeUint64(), 8))) 460 } 461 462 func (d *Decoder) kUint16(f *codecFnInfo, rv reflect.Value) { 463 rvSetUint16(rv, uint16(chkOvf.UintV(d.d.DecodeUint64(), 16))) 464 } 465 466 func (d *Decoder) kUint32(f *codecFnInfo, rv reflect.Value) { 467 rvSetUint32(rv, uint32(chkOvf.UintV(d.d.DecodeUint64(), 32))) 468 } 469 470 func (d *Decoder) kUint64(f *codecFnInfo, rv reflect.Value) { 471 rvSetUint64(rv, d.d.DecodeUint64()) 472 } 473 474 func (d *Decoder) kInterfaceNaked(f *codecFnInfo) (rvn reflect.Value) { 475 // nil interface: 476 // use some hieristics to decode it appropriately 477 // based on the detected next value in the stream. 478 n := d.naked() 479 d.d.DecodeNaked() 480 481 // We cannot decode non-nil stream value into nil interface with methods (e.g. io.Reader). 482 // Howver, it is possible that the user has ways to pass in a type for a given interface 483 // - MapType 484 // - SliceType 485 // - Extensions 486 // 487 // Consequently, we should relax this. Put it behind a const flag for now. 488 if decFailNonEmptyIntf && f.ti.numMeth > 0 { 489 d.errorf("cannot decode non-nil codec value into nil %v (%v methods)", f.ti.rt, f.ti.numMeth) 490 } 491 switch n.v { 492 case valueTypeMap: 493 mtid := d.mtid 494 if mtid == 0 { 495 if d.jsms { // if json, default to a map type with string keys 496 mtid = mapStrIntfTypId // for json performance 497 } else { 498 mtid = mapIntfIntfTypId 499 } 500 } 501 if mtid == mapStrIntfTypId { 502 var v2 map[string]interface{} 503 d.decode(&v2) 504 rvn = rv4iptr(&v2).Elem() 505 } else if mtid == mapIntfIntfTypId { 506 var v2 map[interface{}]interface{} 507 d.decode(&v2) 508 rvn = rv4iptr(&v2).Elem() 509 } else if d.mtr { 510 rvn = reflect.New(d.h.MapType) 511 d.decode(rv2i(rvn)) 512 rvn = rvn.Elem() 513 } else { 514 rvn = rvZeroAddrK(d.h.MapType, reflect.Map) 515 d.decodeValue(rvn, nil) 516 } 517 case valueTypeArray: 518 if d.stid == 0 || d.stid == intfSliceTypId { 519 var v2 []interface{} 520 d.decode(&v2) 521 rvn = rv4iptr(&v2).Elem() 522 } else if d.str { 523 rvn = reflect.New(d.h.SliceType) 524 d.decode(rv2i(rvn)) 525 rvn = rvn.Elem() 526 } else { 527 rvn = rvZeroAddrK(d.h.SliceType, reflect.Slice) 528 d.decodeValue(rvn, nil) 529 } 530 if reflectArrayOfSupported && d.h.PreferArrayOverSlice { 531 rvn = rvGetArray4Slice(rvn) 532 } 533 case valueTypeExt: 534 tag, bytes := n.u, n.l // calling decode below might taint the values 535 bfn := d.h.getExtForTag(tag) 536 var re = RawExt{Tag: tag} 537 if bytes == nil { 538 // it is one of the InterfaceExt ones: json and cbor. 539 // most likely cbor, as json decoding never reveals valueTypeExt (no tagging support) 540 if bfn == nil { 541 d.decode(&re.Value) 542 rvn = rv4iptr(&re).Elem() 543 } else { 544 if bfn.ext == SelfExt { 545 rvn = rvZeroAddrK(bfn.rt, bfn.rt.Kind()) 546 d.decodeValue(rvn, d.h.fnNoExt(bfn.rt)) 547 } else { 548 rvn = reflect.New(bfn.rt) 549 d.interfaceExtConvertAndDecode(rv2i(rvn), bfn.ext) 550 rvn = rvn.Elem() 551 } 552 } 553 } else { 554 // one of the BytesExt ones: binc, msgpack, simple 555 if bfn == nil { 556 re.setData(bytes, false) 557 rvn = rv4iptr(&re).Elem() 558 } else { 559 rvn = reflect.New(bfn.rt) 560 if bfn.ext == SelfExt { 561 d.sideDecode(rv2i(rvn), bfn.rt, bytes) 562 } else { 563 bfn.ext.ReadExt(rv2i(rvn), bytes) 564 } 565 rvn = rvn.Elem() 566 } 567 } 568 // if struct/array, directly store pointer into the interface 569 if d.h.PreferPointerForStructOrArray && rvn.CanAddr() { 570 if rk := rvn.Kind(); rk == reflect.Array || rk == reflect.Struct { 571 rvn = rvn.Addr() 572 } 573 } 574 case valueTypeNil: 575 // rvn = reflect.Zero(f.ti.rt) 576 // no-op 577 case valueTypeInt: 578 rvn = n.ri() 579 case valueTypeUint: 580 rvn = n.ru() 581 case valueTypeFloat: 582 rvn = n.rf() 583 case valueTypeBool: 584 rvn = n.rb() 585 case valueTypeString, valueTypeSymbol: 586 rvn = n.rs() 587 case valueTypeBytes: 588 rvn = n.rl() 589 case valueTypeTime: 590 rvn = n.rt() 591 default: 592 halt.errorf("kInterfaceNaked: unexpected valueType: %d", n.v) 593 } 594 return 595 } 596 597 func (d *Decoder) kInterface(f *codecFnInfo, rv reflect.Value) { 598 // Note: A consequence of how kInterface works, is that 599 // if an interface already contains something, we try 600 // to decode into what was there before. 601 // We do not replace with a generic value (as got from decodeNaked). 602 // 603 // every interface passed here MUST be settable. 604 // 605 // ensure you call rvSetIntf(...) before returning. 606 607 isnilrv := rvIsNil(rv) 608 609 var rvn reflect.Value 610 611 if d.h.InterfaceReset { 612 // check if mapping to a type: if so, initialize it and move on 613 rvn = d.h.intf2impl(f.ti.rtid) 614 if !rvn.IsValid() { 615 rvn = d.kInterfaceNaked(f) 616 if rvn.IsValid() { 617 rvSetIntf(rv, rvn) 618 } else if !isnilrv { 619 decSetNonNilRV2Zero4Intf(rv) 620 } 621 return 622 } 623 } else if isnilrv { 624 // check if mapping to a type: if so, initialize it and move on 625 rvn = d.h.intf2impl(f.ti.rtid) 626 if !rvn.IsValid() { 627 rvn = d.kInterfaceNaked(f) 628 if rvn.IsValid() { 629 rvSetIntf(rv, rvn) 630 } 631 return 632 } 633 } else { 634 // now we have a non-nil interface value, meaning it contains a type 635 rvn = rv.Elem() 636 } 637 638 // rvn is now a non-interface type 639 640 canDecode, _ := isDecodeable(rvn) 641 642 // Note: interface{} is settable, but underlying type may not be. 643 // Consequently, we MAY have to allocate a value (containing the underlying value), 644 // decode into it, and reset the interface to that new value. 645 646 if !canDecode { 647 rvn2 := d.oneShotAddrRV(rvn.Type(), rvn.Kind()) 648 rvSetDirect(rvn2, rvn) 649 rvn = rvn2 650 } 651 652 d.decodeValue(rvn, nil) 653 rvSetIntf(rv, rvn) 654 } 655 656 func decStructFieldKeyNotString(dd decDriver, keyType valueType, b *[decScratchByteArrayLen]byte) (rvkencname []byte) { 657 if keyType == valueTypeInt { 658 rvkencname = strconv.AppendInt(b[:0], dd.DecodeInt64(), 10) 659 } else if keyType == valueTypeUint { 660 rvkencname = strconv.AppendUint(b[:0], dd.DecodeUint64(), 10) 661 } else if keyType == valueTypeFloat { 662 rvkencname = strconv.AppendFloat(b[:0], dd.DecodeFloat64(), 'f', -1, 64) 663 } else { 664 halt.errorf("invalid struct key type: %v", keyType) 665 } 666 return 667 } 668 669 func (d *Decoder) kStructField(si *structFieldInfo, rv reflect.Value) { 670 if d.d.TryNil() { 671 if rv = si.path.field(rv); rv.IsValid() { 672 decSetNonNilRV2Zero(rv) 673 } 674 return 675 } 676 d.decodeValueNoCheckNil(si.path.fieldAlloc(rv), nil) 677 } 678 679 func (d *Decoder) kStruct(f *codecFnInfo, rv reflect.Value) { 680 ctyp := d.d.ContainerType() 681 ti := f.ti 682 var mf MissingFielder 683 if ti.flagMissingFielder { 684 mf = rv2i(rv).(MissingFielder) 685 } else if ti.flagMissingFielderPtr { 686 mf = rv2i(rvAddr(rv, ti.ptr)).(MissingFielder) 687 } 688 if ctyp == valueTypeMap { 689 containerLen := d.mapStart(d.d.ReadMapStart()) 690 if containerLen == 0 { 691 d.mapEnd() 692 return 693 } 694 hasLen := containerLen >= 0 695 var name2 []byte 696 if mf != nil { 697 var namearr2 [16]byte 698 name2 = namearr2[:0] 699 } 700 var rvkencname []byte 701 for j := 0; d.containerNext(j, containerLen, hasLen); j++ { 702 d.mapElemKey() 703 if ti.keyType == valueTypeString { 704 rvkencname = d.d.DecodeStringAsBytes() 705 } else { 706 rvkencname = decStructFieldKeyNotString(d.d, ti.keyType, &d.b) 707 } 708 d.mapElemValue() 709 if si := ti.siForEncName(rvkencname); si != nil { 710 d.kStructField(si, rv) 711 } else if mf != nil { 712 // store rvkencname in new []byte, as it previously shares Decoder.b, which is used in decode 713 name2 = append(name2[:0], rvkencname...) 714 var f interface{} 715 d.decode(&f) 716 if !mf.CodecMissingField(name2, f) && d.h.ErrorIfNoField { 717 d.errorf("no matching struct field when decoding stream map with key: %s ", stringView(name2)) 718 } 719 } else { 720 d.structFieldNotFound(-1, stringView(rvkencname)) 721 } 722 } 723 d.mapEnd() 724 } else if ctyp == valueTypeArray { 725 containerLen := d.arrayStart(d.d.ReadArrayStart()) 726 if containerLen == 0 { 727 d.arrayEnd() 728 return 729 } 730 // Not much gain from doing it two ways for array. 731 // Arrays are not used as much for structs. 732 tisfi := ti.sfi.source() 733 hasLen := containerLen >= 0 734 735 // iterate all the items in the stream 736 // if mapped elem-wise to a field, handle it 737 // if more stream items than can be mapped, error it 738 for j := 0; d.containerNext(j, containerLen, hasLen); j++ { 739 d.arrayElem() 740 if j < len(tisfi) { 741 d.kStructField(tisfi[j], rv) 742 } else { 743 d.structFieldNotFound(j, "") 744 } 745 } 746 747 d.arrayEnd() 748 } else { 749 d.onerror(errNeedMapOrArrayDecodeToStruct) 750 } 751 } 752 753 func (d *Decoder) kSlice(f *codecFnInfo, rv reflect.Value) { 754 // A slice can be set from a map or array in stream. 755 // This way, the order can be kept (as order is lost with map). 756 757 // Note: rv is a slice type here - guaranteed 758 759 ti := f.ti 760 rvCanset := rv.CanSet() 761 762 ctyp := d.d.ContainerType() 763 if ctyp == valueTypeBytes || ctyp == valueTypeString { 764 // you can only decode bytes or string in the stream into a slice or array of bytes 765 if !(ti.rtid == uint8SliceTypId || ti.elemkind == uint8(reflect.Uint8)) { 766 d.errorf("bytes/string in stream must decode into slice/array of bytes, not %v", ti.rt) 767 } 768 rvbs := rvGetBytes(rv) 769 if !rvCanset { 770 // not addressable byte slice, so do not decode into it past the length 771 rvbs = rvbs[:len(rvbs):len(rvbs)] 772 } 773 bs2 := d.decodeBytesInto(rvbs) 774 // if !(len(bs2) == len(rvbs) && byteSliceSameData(rvbs, bs2)) { 775 if !(len(bs2) > 0 && len(bs2) == len(rvbs) && &bs2[0] == &rvbs[0]) { 776 if rvCanset { 777 rvSetBytes(rv, bs2) 778 } else if len(rvbs) > 0 && len(bs2) > 0 { 779 copy(rvbs, bs2) 780 } 781 } 782 return 783 } 784 785 slh, containerLenS := d.decSliceHelperStart() // only expects valueType(Array|Map) - never Nil 786 787 // an array can never return a nil slice. so no need to check f.array here. 788 if containerLenS == 0 { 789 if rvCanset { 790 if rvIsNil(rv) { 791 rvSetDirect(rv, rvSliceZeroCap(ti.rt)) 792 } else { 793 rvSetSliceLen(rv, 0) 794 } 795 } 796 slh.End() 797 return 798 } 799 800 rtelem0Mut := !scalarBitset.isset(ti.elemkind) 801 rtelem := ti.elem 802 803 for k := reflect.Kind(ti.elemkind); k == reflect.Ptr; k = rtelem.Kind() { 804 rtelem = rtelem.Elem() 805 } 806 807 var fn *codecFn 808 809 var rvChanged bool 810 811 var rv0 = rv 812 var rv9 reflect.Value 813 814 rvlen := rvLenSlice(rv) 815 rvcap := rvCapSlice(rv) 816 hasLen := containerLenS > 0 817 if hasLen { 818 if containerLenS > rvcap { 819 oldRvlenGtZero := rvlen > 0 820 rvlen1 := decInferLen(containerLenS, d.h.MaxInitLen, int(ti.elemsize)) 821 if rvlen1 == rvlen { 822 } else if rvlen1 <= rvcap { 823 if rvCanset { 824 rvlen = rvlen1 825 rvSetSliceLen(rv, rvlen) 826 } 827 } else if rvCanset { // rvlen1 > rvcap 828 rvlen = rvlen1 829 rv, rvCanset = rvMakeSlice(rv, f.ti, rvlen, rvlen) 830 rvcap = rvlen 831 rvChanged = !rvCanset 832 } else { // rvlen1 > rvcap && !canSet 833 d.errorf("cannot decode into non-settable slice") 834 } 835 if rvChanged && oldRvlenGtZero && rtelem0Mut { 836 rvCopySlice(rv, rv0, rtelem) // only copy up to length NOT cap i.e. rv0.Slice(0, rvcap) 837 } 838 } else if containerLenS != rvlen { 839 if rvCanset { 840 rvlen = containerLenS 841 rvSetSliceLen(rv, rvlen) 842 } 843 } 844 } 845 846 // consider creating new element once, and just decoding into it. 847 var elemReset = d.h.SliceElementReset 848 849 var j int 850 851 for ; d.containerNext(j, containerLenS, hasLen); j++ { 852 if j == 0 { 853 if rvIsNil(rv) { // means hasLen = false 854 if rvCanset { 855 rvlen = decInferLen(containerLenS, d.h.MaxInitLen, int(ti.elemsize)) 856 rv, rvCanset = rvMakeSlice(rv, f.ti, rvlen, rvlen) 857 rvcap = rvlen 858 rvChanged = !rvCanset 859 } else { 860 d.errorf("cannot decode into non-settable slice") 861 } 862 } 863 if fn == nil { 864 fn = d.h.fn(rtelem) 865 } 866 } 867 // if indefinite, etc, then expand the slice if necessary 868 if j >= rvlen { 869 slh.ElemContainerState(j) 870 871 // expand the slice up to the cap. 872 // Note that we did, so we have to reset it later. 873 874 if rvlen < rvcap { 875 rvlen = rvcap 876 if rvCanset { 877 rvSetSliceLen(rv, rvlen) 878 } else if rvChanged { 879 rv = rvSlice(rv, rvlen) 880 } else { 881 d.onerror(errExpandSliceCannotChange) 882 } 883 } else { 884 if !(rvCanset || rvChanged) { 885 d.onerror(errExpandSliceCannotChange) 886 } 887 rv, rvcap, rvCanset = rvGrowSlice(rv, f.ti, rvcap, 1) 888 rvlen = rvcap 889 rvChanged = !rvCanset 890 } 891 } else { 892 slh.ElemContainerState(j) 893 } 894 rv9 = rvSliceIndex(rv, j, f.ti) 895 if elemReset { 896 rvSetZero(rv9) 897 } 898 d.decodeValue(rv9, fn) 899 } 900 if j < rvlen { 901 if rvCanset { 902 rvSetSliceLen(rv, j) 903 } else if rvChanged { 904 rv = rvSlice(rv, j) 905 } 906 // rvlen = j 907 } else if j == 0 && rvIsNil(rv) { 908 if rvCanset { 909 rv = rvSliceZeroCap(ti.rt) 910 rvCanset = false 911 rvChanged = true 912 } 913 } 914 slh.End() 915 916 if rvChanged { // infers rvCanset=true, so it can be reset 917 rvSetDirect(rv0, rv) 918 } 919 } 920 921 func (d *Decoder) kArray(f *codecFnInfo, rv reflect.Value) { 922 // An array can be set from a map or array in stream. 923 924 ctyp := d.d.ContainerType() 925 if handleBytesWithinKArray && (ctyp == valueTypeBytes || ctyp == valueTypeString) { 926 // you can only decode bytes or string in the stream into a slice or array of bytes 927 if f.ti.elemkind != uint8(reflect.Uint8) { 928 d.errorf("bytes/string in stream can decode into array of bytes, but not %v", f.ti.rt) 929 } 930 rvbs := rvGetArrayBytes(rv, nil) 931 bs2 := d.decodeBytesInto(rvbs) 932 if !byteSliceSameData(rvbs, bs2) && len(rvbs) > 0 && len(bs2) > 0 { 933 copy(rvbs, bs2) 934 } 935 return 936 } 937 938 slh, containerLenS := d.decSliceHelperStart() // only expects valueType(Array|Map) - never Nil 939 940 // an array can never return a nil slice. so no need to check f.array here. 941 if containerLenS == 0 { 942 slh.End() 943 return 944 } 945 946 rtelem := f.ti.elem 947 for k := reflect.Kind(f.ti.elemkind); k == reflect.Ptr; k = rtelem.Kind() { 948 rtelem = rtelem.Elem() 949 } 950 951 var fn *codecFn 952 953 var rv9 reflect.Value 954 955 rvlen := rv.Len() // same as cap 956 hasLen := containerLenS > 0 957 if hasLen && containerLenS > rvlen { 958 d.errorf("cannot decode into array with length: %v, less than container length: %v", rvlen, containerLenS) 959 } 960 961 // consider creating new element once, and just decoding into it. 962 var elemReset = d.h.SliceElementReset 963 964 for j := 0; d.containerNext(j, containerLenS, hasLen); j++ { 965 // note that you cannot expand the array if indefinite and we go past array length 966 if j >= rvlen { 967 slh.arrayCannotExpand(hasLen, rvlen, j, containerLenS) 968 return 969 } 970 971 slh.ElemContainerState(j) 972 rv9 = rvArrayIndex(rv, j, f.ti) 973 if elemReset { 974 rvSetZero(rv9) 975 } 976 977 if fn == nil { 978 fn = d.h.fn(rtelem) 979 } 980 d.decodeValue(rv9, fn) 981 } 982 slh.End() 983 } 984 985 func (d *Decoder) kChan(f *codecFnInfo, rv reflect.Value) { 986 // A slice can be set from a map or array in stream. 987 // This way, the order can be kept (as order is lost with map). 988 989 ti := f.ti 990 if ti.chandir&uint8(reflect.SendDir) == 0 { 991 d.errorf("receive-only channel cannot be decoded") 992 } 993 ctyp := d.d.ContainerType() 994 if ctyp == valueTypeBytes || ctyp == valueTypeString { 995 // you can only decode bytes or string in the stream into a slice or array of bytes 996 if !(ti.rtid == uint8SliceTypId || ti.elemkind == uint8(reflect.Uint8)) { 997 d.errorf("bytes/string in stream must decode into slice/array of bytes, not %v", ti.rt) 998 } 999 bs2 := d.d.DecodeBytes(nil) 1000 irv := rv2i(rv) 1001 ch, ok := irv.(chan<- byte) 1002 if !ok { 1003 ch = irv.(chan byte) 1004 } 1005 for _, b := range bs2 { 1006 ch <- b 1007 } 1008 return 1009 } 1010 1011 var rvCanset = rv.CanSet() 1012 1013 // only expects valueType(Array|Map - nil handled above) 1014 slh, containerLenS := d.decSliceHelperStart() 1015 1016 // an array can never return a nil slice. so no need to check f.array here. 1017 if containerLenS == 0 { 1018 if rvCanset && rvIsNil(rv) { 1019 rvSetDirect(rv, reflect.MakeChan(ti.rt, 0)) 1020 } 1021 slh.End() 1022 return 1023 } 1024 1025 rtelem := ti.elem 1026 useTransient := decUseTransient && ti.elemkind != byte(reflect.Ptr) && ti.tielem.flagCanTransient 1027 1028 for k := reflect.Kind(ti.elemkind); k == reflect.Ptr; k = rtelem.Kind() { 1029 rtelem = rtelem.Elem() 1030 } 1031 1032 var fn *codecFn 1033 1034 var rvChanged bool 1035 var rv0 = rv 1036 var rv9 reflect.Value 1037 1038 var rvlen int // = rv.Len() 1039 hasLen := containerLenS > 0 1040 1041 for j := 0; d.containerNext(j, containerLenS, hasLen); j++ { 1042 if j == 0 { 1043 if rvIsNil(rv) { 1044 if hasLen { 1045 rvlen = decInferLen(containerLenS, d.h.MaxInitLen, int(ti.elemsize)) 1046 } else { 1047 rvlen = decDefChanCap 1048 } 1049 if rvCanset { 1050 rv = reflect.MakeChan(ti.rt, rvlen) 1051 rvChanged = true 1052 } else { 1053 d.errorf("cannot decode into non-settable chan") 1054 } 1055 } 1056 if fn == nil { 1057 fn = d.h.fn(rtelem) 1058 } 1059 } 1060 slh.ElemContainerState(j) 1061 if rv9.IsValid() { 1062 rvSetZero(rv9) 1063 } else if decUseTransient && useTransient { 1064 rv9 = d.perType.TransientAddrK(ti.elem, reflect.Kind(ti.elemkind)) 1065 } else { 1066 rv9 = rvZeroAddrK(ti.elem, reflect.Kind(ti.elemkind)) 1067 } 1068 if !d.d.TryNil() { 1069 d.decodeValueNoCheckNil(rv9, fn) 1070 } 1071 rv.Send(rv9) 1072 } 1073 slh.End() 1074 1075 if rvChanged { // infers rvCanset=true, so it can be reset 1076 rvSetDirect(rv0, rv) 1077 } 1078 1079 } 1080 1081 func (d *Decoder) kMap(f *codecFnInfo, rv reflect.Value) { 1082 containerLen := d.mapStart(d.d.ReadMapStart()) 1083 ti := f.ti 1084 if rvIsNil(rv) { 1085 rvlen := decInferLen(containerLen, d.h.MaxInitLen, int(ti.keysize+ti.elemsize)) 1086 rvSetDirect(rv, makeMapReflect(ti.rt, rvlen)) 1087 } 1088 1089 if containerLen == 0 { 1090 d.mapEnd() 1091 return 1092 } 1093 1094 ktype, vtype := ti.key, ti.elem 1095 ktypeId := rt2id(ktype) 1096 vtypeKind := reflect.Kind(ti.elemkind) 1097 ktypeKind := reflect.Kind(ti.keykind) 1098 kfast := mapKeyFastKindFor(ktypeKind) 1099 visindirect := mapStoresElemIndirect(uintptr(ti.elemsize)) 1100 visref := refBitset.isset(ti.elemkind) 1101 1102 vtypePtr := vtypeKind == reflect.Ptr 1103 ktypePtr := ktypeKind == reflect.Ptr 1104 1105 vTransient := decUseTransient && !vtypePtr && ti.tielem.flagCanTransient 1106 kTransient := decUseTransient && !ktypePtr && ti.tikey.flagCanTransient 1107 1108 var vtypeElem reflect.Type 1109 1110 var keyFn, valFn *codecFn 1111 var ktypeLo, vtypeLo = ktype, vtype 1112 1113 if ktypeKind == reflect.Ptr { 1114 for ktypeLo = ktype.Elem(); ktypeLo.Kind() == reflect.Ptr; ktypeLo = ktypeLo.Elem() { 1115 } 1116 } 1117 1118 if vtypePtr { 1119 vtypeElem = vtype.Elem() 1120 for vtypeLo = vtypeElem; vtypeLo.Kind() == reflect.Ptr; vtypeLo = vtypeLo.Elem() { 1121 } 1122 } 1123 1124 rvkMut := !scalarBitset.isset(ti.keykind) // if ktype is immutable, then re-use the same rvk. 1125 rvvMut := !scalarBitset.isset(ti.elemkind) 1126 rvvCanNil := isnilBitset.isset(ti.elemkind) 1127 1128 // rvk: key 1129 // rvkn: if non-mutable, on each iteration of loop, set rvk to this 1130 // rvv: value 1131 // rvvn: if non-mutable, on each iteration of loop, set rvv to this 1132 // if mutable, may be used as a temporary value for local-scoped operations 1133 // rvva: if mutable, used as transient value for use for key lookup 1134 // rvvz: zero value of map value type, used to do a map set when nil is found in stream 1135 var rvk, rvkn, rvv, rvvn, rvva, rvvz reflect.Value 1136 1137 // we do a doMapGet if kind is mutable, and InterfaceReset=true if interface 1138 var doMapGet, doMapSet bool 1139 1140 if !d.h.MapValueReset { 1141 if rvvMut && (vtypeKind != reflect.Interface || !d.h.InterfaceReset) { 1142 doMapGet = true 1143 rvva = mapAddrLoopvarRV(vtype, vtypeKind) 1144 } 1145 } 1146 1147 ktypeIsString := ktypeId == stringTypId 1148 ktypeIsIntf := ktypeId == intfTypId 1149 1150 hasLen := containerLen > 0 1151 1152 // kstrbs is used locally for the key bytes, so we can reduce allocation. 1153 // When we read keys, we copy to this local bytes array, and use a stringView for lookup. 1154 // We only convert it into a true string if we have to do a set on the map. 1155 1156 // Since kstr2bs will usually escape to the heap, declaring a [64]byte array may be wasteful. 1157 // It is only valuable if we are sure that it is declared on the stack. 1158 // var kstrarr [64]byte // most keys are less than 32 bytes, and even more less than 64 1159 // var kstrbs = kstrarr[:0] 1160 var kstrbs []byte 1161 var kstr2bs []byte 1162 var s string 1163 1164 var callFnRvk bool 1165 1166 fnRvk2 := func() (s string) { 1167 callFnRvk = false 1168 if len(kstr2bs) < 2 { 1169 return string(kstr2bs) 1170 } 1171 return d.mapKeyString(&callFnRvk, &kstrbs, &kstr2bs) 1172 } 1173 1174 // Use a possibly transient (map) value (and key), to reduce allocation 1175 1176 for j := 0; d.containerNext(j, containerLen, hasLen); j++ { 1177 callFnRvk = false 1178 if j == 0 { 1179 // if vtypekind is a scalar and thus value will be decoded using TransientAddrK, 1180 // then it is ok to use TransientAddr2K for the map key. 1181 if decUseTransient && vTransient && kTransient { 1182 rvk = d.perType.TransientAddr2K(ktype, ktypeKind) 1183 } else { 1184 rvk = rvZeroAddrK(ktype, ktypeKind) 1185 } 1186 if !rvkMut { 1187 rvkn = rvk 1188 } 1189 if !rvvMut { 1190 if decUseTransient && vTransient { 1191 rvvn = d.perType.TransientAddrK(vtype, vtypeKind) 1192 } else { 1193 rvvn = rvZeroAddrK(vtype, vtypeKind) 1194 } 1195 } 1196 if !ktypeIsString && keyFn == nil { 1197 keyFn = d.h.fn(ktypeLo) 1198 } 1199 if valFn == nil { 1200 valFn = d.h.fn(vtypeLo) 1201 } 1202 } else if rvkMut { 1203 rvSetZero(rvk) 1204 } else { 1205 rvk = rvkn 1206 } 1207 1208 d.mapElemKey() 1209 if ktypeIsString { 1210 kstr2bs = d.d.DecodeStringAsBytes() 1211 rvSetString(rvk, fnRvk2()) 1212 } else { 1213 d.decByteState = decByteStateNone 1214 d.decodeValue(rvk, keyFn) 1215 // special case if interface wrapping a byte slice 1216 if ktypeIsIntf { 1217 if rvk2 := rvk.Elem(); rvk2.IsValid() && rvk2.Type() == uint8SliceTyp { 1218 kstr2bs = rvGetBytes(rvk2) 1219 rvSetIntf(rvk, rv4istr(fnRvk2())) 1220 } 1221 // NOTE: consider failing early if map/slice/func 1222 } 1223 } 1224 1225 d.mapElemValue() 1226 1227 if d.d.TryNil() { 1228 // since a map, we have to set zero value if needed 1229 if !rvvz.IsValid() { 1230 rvvz = rvZeroK(vtype, vtypeKind) 1231 } 1232 if callFnRvk { 1233 s = d.string(kstr2bs) 1234 if ktypeIsString { 1235 rvSetString(rvk, s) 1236 } else { // ktypeIsIntf 1237 rvSetIntf(rvk, rv4istr(s)) 1238 } 1239 } 1240 mapSet(rv, rvk, rvvz, kfast, visindirect, visref) 1241 continue 1242 } 1243 1244 // there is non-nil content in the stream to decode ... 1245 // consequently, it's ok to just directly create new value to the pointer (if vtypePtr) 1246 1247 // set doMapSet to false iff u do a get, and the return value is a non-nil pointer 1248 doMapSet = true 1249 1250 if !rvvMut { 1251 rvv = rvvn 1252 } else if !doMapGet { 1253 goto NEW_RVV 1254 } else { 1255 rvv = mapGet(rv, rvk, rvva, kfast, visindirect, visref) 1256 if !rvv.IsValid() || (rvvCanNil && rvIsNil(rvv)) { 1257 goto NEW_RVV 1258 } 1259 switch vtypeKind { 1260 case reflect.Ptr, reflect.Map: // ok to decode directly into map 1261 doMapSet = false 1262 case reflect.Interface: 1263 // if an interface{}, just decode into it iff a non-nil ptr/map, else allocate afresh 1264 rvvn = rvv.Elem() 1265 if k := rvvn.Kind(); (k == reflect.Ptr || k == reflect.Map) && !rvIsNil(rvvn) { 1266 d.decodeValueNoCheckNil(rvvn, nil) // valFn is incorrect here 1267 continue 1268 } 1269 // make addressable (so we can set the interface) 1270 rvvn = rvZeroAddrK(vtype, vtypeKind) 1271 rvSetIntf(rvvn, rvv) 1272 rvv = rvvn 1273 default: 1274 // make addressable (so you can set the slice/array elements, etc) 1275 if decUseTransient && vTransient { 1276 rvvn = d.perType.TransientAddrK(vtype, vtypeKind) 1277 } else { 1278 rvvn = rvZeroAddrK(vtype, vtypeKind) 1279 } 1280 rvSetDirect(rvvn, rvv) 1281 rvv = rvvn 1282 } 1283 } 1284 goto DECODE_VALUE_NO_CHECK_NIL 1285 1286 NEW_RVV: 1287 if vtypePtr { 1288 rvv = reflect.New(vtypeElem) // non-nil in stream, so allocate value 1289 } else if decUseTransient && vTransient { 1290 rvv = d.perType.TransientAddrK(vtype, vtypeKind) 1291 } else { 1292 rvv = rvZeroAddrK(vtype, vtypeKind) 1293 } 1294 1295 DECODE_VALUE_NO_CHECK_NIL: 1296 d.decodeValueNoCheckNil(rvv, valFn) 1297 1298 if doMapSet { 1299 if callFnRvk { 1300 s = d.string(kstr2bs) 1301 if ktypeIsString { 1302 rvSetString(rvk, s) 1303 } else { // ktypeIsIntf 1304 rvSetIntf(rvk, rv4istr(s)) 1305 } 1306 } 1307 mapSet(rv, rvk, rvv, kfast, visindirect, visref) 1308 } 1309 } 1310 1311 d.mapEnd() 1312 } 1313 1314 // Decoder reads and decodes an object from an input stream in a supported format. 1315 // 1316 // Decoder is NOT safe for concurrent use i.e. a Decoder cannot be used 1317 // concurrently in multiple goroutines. 1318 // 1319 // However, as Decoder could be allocation heavy to initialize, a Reset method is provided 1320 // so its state can be reused to decode new input streams repeatedly. 1321 // This is the idiomatic way to use. 1322 type Decoder struct { 1323 panicHdl 1324 1325 d decDriver 1326 1327 // cache the mapTypeId and sliceTypeId for faster comparisons 1328 mtid uintptr 1329 stid uintptr 1330 1331 h *BasicHandle 1332 1333 blist bytesFreelist 1334 1335 // ---- cpu cache line boundary? 1336 decRd 1337 1338 // ---- cpu cache line boundary? 1339 n fauxUnion 1340 1341 hh Handle 1342 err error 1343 1344 perType decPerType 1345 1346 // used for interning strings 1347 is internerMap 1348 1349 // ---- cpu cache line boundary? 1350 // ---- writable fields during execution --- *try* to keep in sep cache line 1351 maxdepth int16 1352 depth int16 1353 1354 // Extensions can call Decode() within a current Decode() call. 1355 // We need to know when the top level Decode() call returns, 1356 // so we can decide whether to Release() or not. 1357 calls uint16 // what depth in mustDecode are we in now. 1358 1359 c containerState 1360 1361 decByteState 1362 1363 // b is an always-available scratch buffer used by Decoder and decDrivers. 1364 // By being always-available, it can be used for one-off things without 1365 // having to get from freelist, use, and return back to freelist. 1366 b [decScratchByteArrayLen]byte 1367 } 1368 1369 // NewDecoder returns a Decoder for decoding a stream of bytes from an io.Reader. 1370 // 1371 // For efficiency, Users are encouraged to configure ReaderBufferSize on the handle 1372 // OR pass in a memory buffered reader (eg bufio.Reader, bytes.Buffer). 1373 func NewDecoder(r io.Reader, h Handle) *Decoder { 1374 d := h.newDecDriver().decoder() 1375 if r != nil { 1376 d.Reset(r) 1377 } 1378 return d 1379 } 1380 1381 // NewDecoderBytes returns a Decoder which efficiently decodes directly 1382 // from a byte slice with zero copying. 1383 func NewDecoderBytes(in []byte, h Handle) *Decoder { 1384 d := h.newDecDriver().decoder() 1385 if in != nil { 1386 d.ResetBytes(in) 1387 } 1388 return d 1389 } 1390 1391 // NewDecoderString returns a Decoder which efficiently decodes directly 1392 // from a string with zero copying. 1393 // 1394 // It is a convenience function that calls NewDecoderBytes with a 1395 // []byte view into the string. 1396 // 1397 // This can be an efficient zero-copy if using default mode i.e. without codec.safe tag. 1398 func NewDecoderString(s string, h Handle) *Decoder { 1399 return NewDecoderBytes(bytesView(s), h) 1400 } 1401 1402 func (d *Decoder) r() *decRd { 1403 return &d.decRd 1404 } 1405 1406 func (d *Decoder) init(h Handle) { 1407 initHandle(h) 1408 d.cbreak = d.js || d.cbor 1409 d.bytes = true 1410 d.err = errDecoderNotInitialized 1411 d.h = h.getBasicHandle() 1412 d.hh = h 1413 d.be = h.isBinary() 1414 if d.h.InternString && d.is == nil { 1415 d.is.init() 1416 } 1417 // NOTE: do not initialize d.n here. It is lazily initialized in d.naked() 1418 } 1419 1420 func (d *Decoder) resetCommon() { 1421 d.d.reset() 1422 d.err = nil 1423 d.c = 0 1424 d.decByteState = decByteStateNone 1425 d.depth = 0 1426 d.calls = 0 1427 // reset all things which were cached from the Handle, but could change 1428 d.maxdepth = decDefMaxDepth 1429 if d.h.MaxDepth > 0 { 1430 d.maxdepth = d.h.MaxDepth 1431 } 1432 d.mtid = 0 1433 d.stid = 0 1434 d.mtr = false 1435 d.str = false 1436 if d.h.MapType != nil { 1437 d.mtid = rt2id(d.h.MapType) 1438 d.mtr = fastpathAvIndex(d.mtid) != -1 1439 } 1440 if d.h.SliceType != nil { 1441 d.stid = rt2id(d.h.SliceType) 1442 d.str = fastpathAvIndex(d.stid) != -1 1443 } 1444 } 1445 1446 // Reset the Decoder with a new Reader to decode from, 1447 // clearing all state from last run(s). 1448 func (d *Decoder) Reset(r io.Reader) { 1449 if r == nil { 1450 r = &eofReader 1451 } 1452 d.bytes = false 1453 if d.ri == nil { 1454 d.ri = new(ioDecReader) 1455 } 1456 d.ri.reset(r, d.h.ReaderBufferSize, &d.blist) 1457 d.decReader = d.ri 1458 d.resetCommon() 1459 } 1460 1461 // ResetBytes resets the Decoder with a new []byte to decode from, 1462 // clearing all state from last run(s). 1463 func (d *Decoder) ResetBytes(in []byte) { 1464 if in == nil { 1465 in = []byte{} 1466 } 1467 d.bytes = true 1468 d.decReader = &d.rb 1469 d.rb.reset(in) 1470 d.resetCommon() 1471 } 1472 1473 // ResetString resets the Decoder with a new string to decode from, 1474 // clearing all state from last run(s). 1475 // 1476 // It is a convenience function that calls ResetBytes with a 1477 // []byte view into the string. 1478 // 1479 // This can be an efficient zero-copy if using default mode i.e. without codec.safe tag. 1480 func (d *Decoder) ResetString(s string) { 1481 d.ResetBytes(bytesView(s)) 1482 } 1483 1484 func (d *Decoder) naked() *fauxUnion { 1485 return &d.n 1486 } 1487 1488 // Decode decodes the stream from reader and stores the result in the 1489 // value pointed to by v. v cannot be a nil pointer. v can also be 1490 // a reflect.Value of a pointer. 1491 // 1492 // Note that a pointer to a nil interface is not a nil pointer. 1493 // If you do not know what type of stream it is, pass in a pointer to a nil interface. 1494 // We will decode and store a value in that nil interface. 1495 // 1496 // Sample usages: 1497 // 1498 // // Decoding into a non-nil typed value 1499 // var f float32 1500 // err = codec.NewDecoder(r, handle).Decode(&f) 1501 // 1502 // // Decoding into nil interface 1503 // var v interface{} 1504 // dec := codec.NewDecoder(r, handle) 1505 // err = dec.Decode(&v) 1506 // 1507 // When decoding into a nil interface{}, we will decode into an appropriate value based 1508 // on the contents of the stream: 1509 // - Numbers are decoded as float64, int64 or uint64. 1510 // - Other values are decoded appropriately depending on the type: 1511 // bool, string, []byte, time.Time, etc 1512 // - Extensions are decoded as RawExt (if no ext function registered for the tag) 1513 // 1514 // Configurations exist on the Handle to override defaults 1515 // (e.g. for MapType, SliceType and how to decode raw bytes). 1516 // 1517 // When decoding into a non-nil interface{} value, the mode of encoding is based on the 1518 // type of the value. When a value is seen: 1519 // - If an extension is registered for it, call that extension function 1520 // - If it implements BinaryUnmarshaler, call its UnmarshalBinary(data []byte) error 1521 // - Else decode it based on its reflect.Kind 1522 // 1523 // There are some special rules when decoding into containers (slice/array/map/struct). 1524 // Decode will typically use the stream contents to UPDATE the container i.e. the values 1525 // in these containers will not be zero'ed before decoding. 1526 // - A map can be decoded from a stream map, by updating matching keys. 1527 // - A slice can be decoded from a stream array, 1528 // by updating the first n elements, where n is length of the stream. 1529 // - A slice can be decoded from a stream map, by decoding as if 1530 // it contains a sequence of key-value pairs. 1531 // - A struct can be decoded from a stream map, by updating matching fields. 1532 // - A struct can be decoded from a stream array, 1533 // by updating fields as they occur in the struct (by index). 1534 // 1535 // This in-place update maintains consistency in the decoding philosophy (i.e. we ALWAYS update 1536 // in place by default). However, the consequence of this is that values in slices or maps 1537 // which are not zero'ed before hand, will have part of the prior values in place after decode 1538 // if the stream doesn't contain an update for those parts. 1539 // 1540 // This in-place update can be disabled by configuring the MapValueReset and SliceElementReset 1541 // decode options available on every handle. 1542 // 1543 // Furthermore, when decoding a stream map or array with length of 0 into a nil map or slice, 1544 // we reset the destination map or slice to a zero-length value. 1545 // 1546 // However, when decoding a stream nil, we reset the destination container 1547 // to its "zero" value (e.g. nil for slice/map, etc). 1548 // 1549 // Note: we allow nil values in the stream anywhere except for map keys. 1550 // A nil value in the encoded stream where a map key is expected is treated as an error. 1551 func (d *Decoder) Decode(v interface{}) (err error) { 1552 // tried to use closure, as runtime optimizes defer with no params. 1553 // This seemed to be causing weird issues (like circular reference found, unexpected panic, etc). 1554 // Also, see https://github.com/golang/go/issues/14939#issuecomment-417836139 1555 if !debugging { 1556 defer func() { 1557 if x := recover(); x != nil { 1558 panicValToErr(d, x, &d.err) 1559 err = d.err 1560 } 1561 }() 1562 } 1563 1564 d.MustDecode(v) 1565 return 1566 } 1567 1568 // MustDecode is like Decode, but panics if unable to Decode. 1569 // 1570 // Note: This provides insight to the code location that triggered the error. 1571 func (d *Decoder) MustDecode(v interface{}) { 1572 halt.onerror(d.err) 1573 if d.hh == nil { 1574 halt.onerror(errNoFormatHandle) 1575 } 1576 1577 // Top-level: v is a pointer and not nil. 1578 d.calls++ 1579 d.decode(v) 1580 d.calls-- 1581 } 1582 1583 // Release releases shared (pooled) resources. 1584 // 1585 // It is important to call Release() when done with a Decoder, so those resources 1586 // are released instantly for use by subsequently created Decoders. 1587 // 1588 // By default, Release() is automatically called unless the option ExplicitRelease is set. 1589 // 1590 // Deprecated: Release is a no-op as pooled resources are not used with an Decoder. 1591 // This method is kept for compatibility reasons only. 1592 func (d *Decoder) Release() { 1593 } 1594 1595 func (d *Decoder) swallow() { 1596 d.d.nextValueBytes(nil) 1597 } 1598 1599 func (d *Decoder) swallowErr() (err error) { 1600 if !debugging { 1601 defer func() { 1602 if x := recover(); x != nil { 1603 panicValToErr(d, x, &err) 1604 } 1605 }() 1606 } 1607 d.swallow() 1608 return 1609 } 1610 1611 func setZero(iv interface{}) { 1612 if iv == nil { 1613 return 1614 } 1615 rv, ok := isNil(iv) 1616 if ok { 1617 return 1618 } 1619 // var canDecode bool 1620 switch v := iv.(type) { 1621 case *string: 1622 *v = "" 1623 case *bool: 1624 *v = false 1625 case *int: 1626 *v = 0 1627 case *int8: 1628 *v = 0 1629 case *int16: 1630 *v = 0 1631 case *int32: 1632 *v = 0 1633 case *int64: 1634 *v = 0 1635 case *uint: 1636 *v = 0 1637 case *uint8: 1638 *v = 0 1639 case *uint16: 1640 *v = 0 1641 case *uint32: 1642 *v = 0 1643 case *uint64: 1644 *v = 0 1645 case *float32: 1646 *v = 0 1647 case *float64: 1648 *v = 0 1649 case *complex64: 1650 *v = 0 1651 case *complex128: 1652 *v = 0 1653 case *[]byte: 1654 *v = nil 1655 case *Raw: 1656 *v = nil 1657 case *time.Time: 1658 *v = time.Time{} 1659 case reflect.Value: 1660 decSetNonNilRV2Zero(v) 1661 default: 1662 if !fastpathDecodeSetZeroTypeSwitch(iv) { 1663 decSetNonNilRV2Zero(rv) 1664 } 1665 } 1666 } 1667 1668 // decSetNonNilRV2Zero will set the non-nil value to its zero value. 1669 func decSetNonNilRV2Zero(v reflect.Value) { 1670 // If not decodeable (settable), we do not touch it. 1671 // We considered empty'ing it if not decodeable e.g. 1672 // - if chan, drain it 1673 // - if map, clear it 1674 // - if slice or array, zero all elements up to len 1675 // 1676 // However, we decided instead that we either will set the 1677 // whole value to the zero value, or leave AS IS. 1678 1679 k := v.Kind() 1680 if k == reflect.Interface { 1681 decSetNonNilRV2Zero4Intf(v) 1682 } else if k == reflect.Ptr { 1683 decSetNonNilRV2Zero4Ptr(v) 1684 } else if v.CanSet() { 1685 rvSetDirectZero(v) 1686 } 1687 } 1688 1689 func decSetNonNilRV2Zero4Ptr(v reflect.Value) { 1690 ve := v.Elem() 1691 if ve.CanSet() { 1692 rvSetZero(ve) // we can have a pointer to an interface 1693 } else if v.CanSet() { 1694 rvSetZero(v) 1695 } 1696 } 1697 1698 func decSetNonNilRV2Zero4Intf(v reflect.Value) { 1699 ve := v.Elem() 1700 if ve.CanSet() { 1701 rvSetDirectZero(ve) // interfaces always have element as a non-interface 1702 } else if v.CanSet() { 1703 rvSetZero(v) 1704 } 1705 } 1706 1707 func (d *Decoder) decode(iv interface{}) { 1708 // a switch with only concrete types can be optimized. 1709 // consequently, we deal with nil and interfaces outside the switch. 1710 1711 if iv == nil { 1712 d.onerror(errCannotDecodeIntoNil) 1713 } 1714 1715 switch v := iv.(type) { 1716 // case nil: 1717 // case Selfer: 1718 case reflect.Value: 1719 if x, _ := isDecodeable(v); !x { 1720 d.haltAsNotDecodeable(v) 1721 } 1722 d.decodeValue(v, nil) 1723 case *string: 1724 *v = d.stringZC(d.d.DecodeStringAsBytes()) 1725 case *bool: 1726 *v = d.d.DecodeBool() 1727 case *int: 1728 *v = int(chkOvf.IntV(d.d.DecodeInt64(), intBitsize)) 1729 case *int8: 1730 *v = int8(chkOvf.IntV(d.d.DecodeInt64(), 8)) 1731 case *int16: 1732 *v = int16(chkOvf.IntV(d.d.DecodeInt64(), 16)) 1733 case *int32: 1734 *v = int32(chkOvf.IntV(d.d.DecodeInt64(), 32)) 1735 case *int64: 1736 *v = d.d.DecodeInt64() 1737 case *uint: 1738 *v = uint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize)) 1739 case *uint8: 1740 *v = uint8(chkOvf.UintV(d.d.DecodeUint64(), 8)) 1741 case *uint16: 1742 *v = uint16(chkOvf.UintV(d.d.DecodeUint64(), 16)) 1743 case *uint32: 1744 *v = uint32(chkOvf.UintV(d.d.DecodeUint64(), 32)) 1745 case *uint64: 1746 *v = d.d.DecodeUint64() 1747 case *float32: 1748 *v = d.decodeFloat32() 1749 case *float64: 1750 *v = d.d.DecodeFloat64() 1751 case *complex64: 1752 *v = complex(d.decodeFloat32(), 0) 1753 case *complex128: 1754 *v = complex(d.d.DecodeFloat64(), 0) 1755 case *[]byte: 1756 *v = d.decodeBytesInto(*v) 1757 case []byte: 1758 // not addressable byte slice, so do not decode into it past the length 1759 b := d.decodeBytesInto(v[:len(v):len(v)]) 1760 if !(len(b) > 0 && len(b) == len(v) && &b[0] == &v[0]) { // not same slice 1761 copy(v, b) 1762 } 1763 case *time.Time: 1764 *v = d.d.DecodeTime() 1765 case *Raw: 1766 *v = d.rawBytes() 1767 1768 case *interface{}: 1769 d.decodeValue(rv4iptr(v), nil) 1770 1771 default: 1772 // we can't check non-predefined types, as they might be a Selfer or extension. 1773 if skipFastpathTypeSwitchInDirectCall || !fastpathDecodeTypeSwitch(iv, d) { 1774 v := reflect.ValueOf(iv) 1775 if x, _ := isDecodeable(v); !x { 1776 d.haltAsNotDecodeable(v) 1777 } 1778 d.decodeValue(v, nil) 1779 } 1780 } 1781 } 1782 1783 // decodeValue MUST be called by the actual value we want to decode into, 1784 // not its addr or a reference to it. 1785 // 1786 // This way, we know if it is itself a pointer, and can handle nil in 1787 // the stream effectively. 1788 // 1789 // Note that decodeValue will handle nil in the stream early, so that the 1790 // subsequent calls i.e. kXXX methods, etc do not have to handle it themselves. 1791 func (d *Decoder) decodeValue(rv reflect.Value, fn *codecFn) { 1792 if d.d.TryNil() { 1793 decSetNonNilRV2Zero(rv) 1794 return 1795 } 1796 d.decodeValueNoCheckNil(rv, fn) 1797 } 1798 1799 func (d *Decoder) decodeValueNoCheckNil(rv reflect.Value, fn *codecFn) { 1800 // If stream is not containing a nil value, then we can deref to the base 1801 // non-pointer value, and decode into that. 1802 var rvp reflect.Value 1803 var rvpValid bool 1804 PTR: 1805 if rv.Kind() == reflect.Ptr { 1806 rvpValid = true 1807 if rvIsNil(rv) { 1808 rvSetDirect(rv, reflect.New(rv.Type().Elem())) 1809 } 1810 rvp = rv 1811 rv = rv.Elem() 1812 goto PTR 1813 } 1814 1815 if fn == nil { 1816 fn = d.h.fn(rv.Type()) 1817 } 1818 if fn.i.addrD { 1819 if rvpValid { 1820 rv = rvp 1821 } else if rv.CanAddr() { 1822 rv = rvAddr(rv, fn.i.ti.ptr) 1823 } else if fn.i.addrDf { 1824 d.errorf("cannot decode into a non-pointer value") 1825 } 1826 } 1827 fn.fd(d, &fn.i, rv) 1828 } 1829 1830 func (d *Decoder) structFieldNotFound(index int, rvkencname string) { 1831 // Note: rvkencname is used only if there is an error, to pass into d.errorf. 1832 // Consequently, it is ok to pass in a stringView 1833 // Since rvkencname may be a stringView, do NOT pass it to another function. 1834 if d.h.ErrorIfNoField { 1835 if index >= 0 { 1836 d.errorf("no matching struct field found when decoding stream array at index %v", index) 1837 } else if rvkencname != "" { 1838 d.errorf("no matching struct field found when decoding stream map with key " + rvkencname) 1839 } 1840 } 1841 d.swallow() 1842 } 1843 1844 func (d *Decoder) arrayCannotExpand(sliceLen, streamLen int) { 1845 if d.h.ErrorIfNoArrayExpand { 1846 d.errorf("cannot expand array len during decode from %v to %v", sliceLen, streamLen) 1847 } 1848 } 1849 1850 func (d *Decoder) haltAsNotDecodeable(rv reflect.Value) { 1851 if !rv.IsValid() { 1852 d.onerror(errCannotDecodeIntoNil) 1853 } 1854 // check if an interface can be retrieved, before grabbing an interface 1855 if !rv.CanInterface() { 1856 d.errorf("cannot decode into a value without an interface: %v", rv) 1857 } 1858 d.errorf("cannot decode into value of kind: %v, %#v", rv.Kind(), rv2i(rv)) 1859 } 1860 1861 func (d *Decoder) depthIncr() { 1862 d.depth++ 1863 if d.depth >= d.maxdepth { 1864 d.onerror(errMaxDepthExceeded) 1865 } 1866 } 1867 1868 func (d *Decoder) depthDecr() { 1869 d.depth-- 1870 } 1871 1872 // Possibly get an interned version of a string, iff InternString=true and decoding a map key. 1873 // 1874 // This should mostly be used for map keys, where the key type is string. 1875 // This is because keys of a map/struct are typically reused across many objects. 1876 func (d *Decoder) string(v []byte) (s string) { 1877 if d.is == nil || d.c != containerMapKey || len(v) < 2 || len(v) > internMaxStrLen { 1878 return string(v) 1879 } 1880 return d.is.string(v) 1881 } 1882 1883 func (d *Decoder) zerocopy() bool { 1884 return d.bytes && d.h.ZeroCopy 1885 } 1886 1887 // decodeBytesInto is a convenience delegate function to decDriver.DecodeBytes. 1888 // It ensures that `in` is not a nil byte, before calling decDriver.DecodeBytes, 1889 // as decDriver.DecodeBytes treats a nil as a hint to use its internal scratch buffer. 1890 func (d *Decoder) decodeBytesInto(in []byte) (v []byte) { 1891 if in == nil { 1892 in = []byte{} 1893 } 1894 return d.d.DecodeBytes(in) 1895 } 1896 1897 func (d *Decoder) rawBytes() (v []byte) { 1898 // ensure that this is not a view into the bytes 1899 // i.e. if necessary, make new copy always. 1900 v = d.d.nextValueBytes([]byte{}) 1901 if d.bytes && !d.h.ZeroCopy { 1902 vv := make([]byte, len(v)) 1903 copy(vv, v) // using copy here triggers make+copy optimization eliding memclr 1904 v = vv 1905 } 1906 return 1907 } 1908 1909 func (d *Decoder) wrapErr(v error, err *error) { 1910 *err = wrapCodecErr(v, d.hh.Name(), d.NumBytesRead(), false) 1911 } 1912 1913 // NumBytesRead returns the number of bytes read 1914 func (d *Decoder) NumBytesRead() int { 1915 return int(d.r().numread()) 1916 } 1917 1918 // decodeFloat32 will delegate to an appropriate DecodeFloat32 implementation (if exists), 1919 // else if will call DecodeFloat64 and ensure the value doesn't overflow. 1920 // 1921 // Note that we return float64 to reduce unnecessary conversions 1922 func (d *Decoder) decodeFloat32() float32 { 1923 if d.js { 1924 return d.jsondriver().DecodeFloat32() // custom implementation for 32-bit 1925 } 1926 return float32(chkOvf.Float32V(d.d.DecodeFloat64())) 1927 } 1928 1929 // ---- container tracking 1930 // Note: We update the .c after calling the callback. 1931 // This way, the callback can know what the last status was. 1932 1933 // MARKER: do not call mapEnd if mapStart returns containerLenNil. 1934 1935 // MARKER: optimize decoding since all formats do not truly support all decDriver'ish operations. 1936 // - Read(Map|Array)Start is only supported by all formats. 1937 // - CheckBreak is only supported by json and cbor. 1938 // - Read(Map|Array)End is only supported by json. 1939 // - Read(Map|Array)Elem(Kay|Value) is only supported by json. 1940 // Honor these in the code, to reduce the number of interface calls (even if empty). 1941 1942 func (d *Decoder) checkBreak() (v bool) { 1943 // MARKER: jsonDecDriver.CheckBreak() cannot be inlined (over budget inlining cost). 1944 // Consequently, there's no benefit in incurring the cost of this wrapping function. 1945 // It is faster to just call the interface method directly. 1946 1947 // if d.js { 1948 // return d.jsondriver().CheckBreak() 1949 // } 1950 // if d.cbor { 1951 // return d.cbordriver().CheckBreak() 1952 // } 1953 1954 if d.cbreak { 1955 v = d.d.CheckBreak() 1956 } 1957 return 1958 } 1959 1960 func (d *Decoder) containerNext(j, containerLen int, hasLen bool) bool { 1961 // MARKER: keep in sync with gen-helper.go.tmpl 1962 1963 // return (hasLen && j < containerLen) || !(hasLen || slh.d.checkBreak()) 1964 if hasLen { 1965 return j < containerLen 1966 } 1967 return !d.checkBreak() 1968 } 1969 1970 func (d *Decoder) mapStart(v int) int { 1971 if v != containerLenNil { 1972 d.depthIncr() 1973 d.c = containerMapStart 1974 } 1975 return v 1976 } 1977 1978 func (d *Decoder) mapElemKey() { 1979 if d.js { 1980 d.jsondriver().ReadMapElemKey() 1981 } 1982 d.c = containerMapKey 1983 } 1984 1985 func (d *Decoder) mapElemValue() { 1986 if d.js { 1987 d.jsondriver().ReadMapElemValue() 1988 } 1989 d.c = containerMapValue 1990 } 1991 1992 func (d *Decoder) mapEnd() { 1993 if d.js { 1994 d.jsondriver().ReadMapEnd() 1995 } 1996 // d.d.ReadMapEnd() 1997 d.depthDecr() 1998 d.c = 0 1999 } 2000 2001 func (d *Decoder) arrayStart(v int) int { 2002 if v != containerLenNil { 2003 d.depthIncr() 2004 d.c = containerArrayStart 2005 } 2006 return v 2007 } 2008 2009 func (d *Decoder) arrayElem() { 2010 if d.js { 2011 d.jsondriver().ReadArrayElem() 2012 } 2013 d.c = containerArrayElem 2014 } 2015 2016 func (d *Decoder) arrayEnd() { 2017 if d.js { 2018 d.jsondriver().ReadArrayEnd() 2019 } 2020 // d.d.ReadArrayEnd() 2021 d.depthDecr() 2022 d.c = 0 2023 } 2024 2025 func (d *Decoder) interfaceExtConvertAndDecode(v interface{}, ext InterfaceExt) { 2026 // var v interface{} = ext.ConvertExt(rv) 2027 // d.d.decode(&v) 2028 // ext.UpdateExt(rv, v) 2029 2030 // assume v is a pointer: 2031 // - if struct|array, pass as is to ConvertExt 2032 // - else make it non-addressable and pass to ConvertExt 2033 // - make return value from ConvertExt addressable 2034 // - decode into it 2035 // - return the interface for passing into UpdateExt. 2036 // - interface should be a pointer if struct|array, else a value 2037 2038 var s interface{} 2039 rv := reflect.ValueOf(v) 2040 rv2 := rv.Elem() 2041 rvk := rv2.Kind() 2042 if rvk == reflect.Struct || rvk == reflect.Array { 2043 s = ext.ConvertExt(v) 2044 } else { 2045 s = ext.ConvertExt(rv2i(rv2)) 2046 } 2047 rv = reflect.ValueOf(s) 2048 2049 // We cannot use isDecodeable here, as the value converted may be nil, 2050 // or it may not be nil but is not addressable and thus we cannot extend it, etc. 2051 // Instead, we just ensure that the value is addressable. 2052 2053 if !rv.CanAddr() { 2054 rvk = rv.Kind() 2055 rv2 = d.oneShotAddrRV(rv.Type(), rvk) 2056 if rvk == reflect.Interface { 2057 rvSetIntf(rv2, rv) 2058 } else { 2059 rvSetDirect(rv2, rv) 2060 } 2061 rv = rv2 2062 } 2063 2064 d.decodeValue(rv, nil) 2065 ext.UpdateExt(v, rv2i(rv)) 2066 } 2067 2068 func (d *Decoder) sideDecode(v interface{}, basetype reflect.Type, bs []byte) { 2069 // NewDecoderBytes(bs, d.hh).decodeValue(baseRV(v), d.h.fnNoExt(basetype)) 2070 2071 defer func(rb bytesDecReader, bytes bool, 2072 c containerState, dbs decByteState, depth int16, r decReader, state interface{}) { 2073 d.rb = rb 2074 d.bytes = bytes 2075 d.c = c 2076 d.decByteState = dbs 2077 d.depth = depth 2078 d.decReader = r 2079 d.d.restoreState(state) 2080 }(d.rb, d.bytes, d.c, d.decByteState, d.depth, d.decReader, d.d.captureState()) 2081 2082 // d.rb.reset(in) 2083 d.rb = bytesDecReader{bs[:len(bs):len(bs)], 0} 2084 d.bytes = true 2085 d.decReader = &d.rb 2086 d.d.resetState() 2087 d.c = 0 2088 d.decByteState = decByteStateNone 2089 d.depth = 0 2090 2091 // must call using fnNoExt 2092 d.decodeValue(baseRV(v), d.h.fnNoExt(basetype)) 2093 } 2094 2095 func (d *Decoder) fauxUnionReadRawBytes(asString bool) { 2096 if asString || d.h.RawToString { 2097 d.n.v = valueTypeString 2098 // fauxUnion is only used within DecodeNaked calls; consequently, we should try to intern. 2099 d.n.s = d.stringZC(d.d.DecodeBytes(nil)) 2100 } else { 2101 d.n.v = valueTypeBytes 2102 d.n.l = d.d.DecodeBytes([]byte{}) 2103 } 2104 } 2105 2106 func (d *Decoder) oneShotAddrRV(rvt reflect.Type, rvk reflect.Kind) reflect.Value { 2107 if decUseTransient && 2108 (numBoolStrSliceBitset.isset(byte(rvk)) || 2109 ((rvk == reflect.Struct || rvk == reflect.Array) && 2110 d.h.getTypeInfo(rt2id(rvt), rvt).flagCanTransient)) { 2111 return d.perType.TransientAddrK(rvt, rvk) 2112 } 2113 return rvZeroAddrK(rvt, rvk) 2114 } 2115 2116 // -------------------------------------------------- 2117 2118 // decSliceHelper assists when decoding into a slice, from a map or an array in the stream. 2119 // A slice can be set from a map or array in stream. This supports the MapBySlice interface. 2120 // 2121 // Note: if IsNil, do not call ElemContainerState. 2122 type decSliceHelper struct { 2123 d *Decoder 2124 ct valueType 2125 Array bool 2126 IsNil bool 2127 } 2128 2129 func (d *Decoder) decSliceHelperStart() (x decSliceHelper, clen int) { 2130 x.ct = d.d.ContainerType() 2131 x.d = d 2132 switch x.ct { 2133 case valueTypeNil: 2134 x.IsNil = true 2135 case valueTypeArray: 2136 x.Array = true 2137 clen = d.arrayStart(d.d.ReadArrayStart()) 2138 case valueTypeMap: 2139 clen = d.mapStart(d.d.ReadMapStart()) 2140 clen += clen 2141 default: 2142 d.errorf("only encoded map or array can be decoded into a slice (%d)", x.ct) 2143 } 2144 return 2145 } 2146 2147 func (x decSliceHelper) End() { 2148 if x.IsNil { 2149 } else if x.Array { 2150 x.d.arrayEnd() 2151 } else { 2152 x.d.mapEnd() 2153 } 2154 } 2155 2156 func (x decSliceHelper) ElemContainerState(index int) { 2157 // Note: if isnil, clen=0, so we never call into ElemContainerState 2158 2159 if x.Array { 2160 x.d.arrayElem() 2161 } else if index&1 == 0 { // index%2 == 0 { 2162 x.d.mapElemKey() 2163 } else { 2164 x.d.mapElemValue() 2165 } 2166 } 2167 2168 func (x decSliceHelper) arrayCannotExpand(hasLen bool, lenv, j, containerLenS int) { 2169 x.d.arrayCannotExpand(lenv, j+1) 2170 // drain completely and return 2171 x.ElemContainerState(j) 2172 x.d.swallow() 2173 j++ 2174 for ; x.d.containerNext(j, containerLenS, hasLen); j++ { 2175 x.ElemContainerState(j) 2176 x.d.swallow() 2177 } 2178 x.End() 2179 } 2180 2181 // decNextValueBytesHelper helps with NextValueBytes calls. 2182 // 2183 // Typical usage: 2184 // - each Handle's decDriver will implement a high level nextValueBytes, 2185 // which will track the current cursor, delegate to a nextValueBytesR 2186 // method, and then potentially call bytesRdV at the end. 2187 // 2188 // See simple.go for typical usage model. 2189 type decNextValueBytesHelper struct { 2190 d *Decoder 2191 } 2192 2193 func (x decNextValueBytesHelper) append1(v *[]byte, b byte) { 2194 if *v != nil && !x.d.bytes { 2195 *v = append(*v, b) 2196 } 2197 } 2198 2199 func (x decNextValueBytesHelper) appendN(v *[]byte, b ...byte) { 2200 if *v != nil && !x.d.bytes { 2201 *v = append(*v, b...) 2202 } 2203 } 2204 2205 func (x decNextValueBytesHelper) appendS(v *[]byte, b string) { 2206 if *v != nil && !x.d.bytes { 2207 *v = append(*v, b...) 2208 } 2209 } 2210 2211 func (x decNextValueBytesHelper) bytesRdV(v *[]byte, startpos uint) { 2212 if x.d.bytes { 2213 *v = x.d.rb.b[startpos:x.d.rb.c] 2214 } 2215 } 2216 2217 // decNegintPosintFloatNumberHelper is used for formats that are binary 2218 // and have distinct ways of storing positive integers vs negative integers 2219 // vs floats, which are uniquely identified by the byte descriptor. 2220 // 2221 // Currently, these formats are binc, cbor and simple. 2222 type decNegintPosintFloatNumberHelper struct { 2223 d *Decoder 2224 } 2225 2226 func (x decNegintPosintFloatNumberHelper) uint64(ui uint64, neg, ok bool) uint64 { 2227 if ok && !neg { 2228 return ui 2229 } 2230 return x.uint64TryFloat(ok) 2231 } 2232 2233 func (x decNegintPosintFloatNumberHelper) uint64TryFloat(ok bool) (ui uint64) { 2234 if ok { // neg = true 2235 x.d.errorf("assigning negative signed value to unsigned type") 2236 } 2237 f, ok := x.d.d.decFloat() 2238 if ok && f >= 0 && noFrac64(math.Float64bits(f)) { 2239 ui = uint64(f) 2240 } else { 2241 x.d.errorf("invalid number loading uint64, with descriptor: %v", x.d.d.descBd()) 2242 } 2243 return ui 2244 } 2245 2246 func decNegintPosintFloatNumberHelperInt64v(ui uint64, neg, incrIfNeg bool) (i int64) { 2247 if neg && incrIfNeg { 2248 ui++ 2249 } 2250 i = chkOvf.SignedIntV(ui) 2251 if neg { 2252 i = -i 2253 } 2254 return 2255 } 2256 2257 func (x decNegintPosintFloatNumberHelper) int64(ui uint64, neg, ok bool) (i int64) { 2258 if ok { 2259 return decNegintPosintFloatNumberHelperInt64v(ui, neg, x.d.cbor) 2260 } 2261 // return x.int64TryFloat() 2262 // } 2263 // func (x decNegintPosintFloatNumberHelper) int64TryFloat() (i int64) { 2264 f, ok := x.d.d.decFloat() 2265 if ok && noFrac64(math.Float64bits(f)) { 2266 i = int64(f) 2267 } else { 2268 x.d.errorf("invalid number loading uint64, with descriptor: %v", x.d.d.descBd()) 2269 } 2270 return 2271 } 2272 2273 func (x decNegintPosintFloatNumberHelper) float64(f float64, ok bool) float64 { 2274 if ok { 2275 return f 2276 } 2277 return x.float64TryInteger() 2278 } 2279 2280 func (x decNegintPosintFloatNumberHelper) float64TryInteger() float64 { 2281 ui, neg, ok := x.d.d.decInteger() 2282 if !ok { 2283 x.d.errorf("invalid descriptor for float: %v", x.d.d.descBd()) 2284 } 2285 return float64(decNegintPosintFloatNumberHelperInt64v(ui, neg, x.d.cbor)) 2286 } 2287 2288 // isDecodeable checks if value can be decoded into 2289 // 2290 // decode can take any reflect.Value that is a inherently addressable i.e. 2291 // - non-nil chan (we will SEND to it) 2292 // - non-nil slice (we will set its elements) 2293 // - non-nil map (we will put into it) 2294 // - non-nil pointer (we can "update" it) 2295 // - func: no 2296 // - interface: no 2297 // - array: if canAddr=true 2298 // - any other value pointer: if canAddr=true 2299 func isDecodeable(rv reflect.Value) (canDecode bool, reason decNotDecodeableReason) { 2300 switch rv.Kind() { 2301 case reflect.Ptr, reflect.Slice, reflect.Chan, reflect.Map: 2302 canDecode = !rvIsNil(rv) 2303 reason = decNotDecodeableReasonNilReference 2304 case reflect.Func, reflect.Interface, reflect.Invalid, reflect.UnsafePointer: 2305 reason = decNotDecodeableReasonBadKind 2306 default: 2307 canDecode = rv.CanAddr() 2308 reason = decNotDecodeableReasonNonAddrValue 2309 } 2310 return 2311 } 2312 2313 func decByteSlice(r *decRd, clen, maxInitLen int, bs []byte) (bsOut []byte) { 2314 if clen <= 0 { 2315 bsOut = zeroByteSlice 2316 } else if cap(bs) >= clen { 2317 bsOut = bs[:clen] 2318 r.readb(bsOut) 2319 } else { 2320 var len2 int 2321 for len2 < clen { 2322 len3 := decInferLen(clen-len2, maxInitLen, 1) 2323 bs3 := bsOut 2324 bsOut = make([]byte, len2+len3) 2325 copy(bsOut, bs3) 2326 r.readb(bsOut[len2:]) 2327 len2 += len3 2328 } 2329 } 2330 return 2331 } 2332 2333 // decInferLen will infer a sensible length, given the following: 2334 // - clen: length wanted. 2335 // - maxlen: max length to be returned. 2336 // if <= 0, it is unset, and we infer it based on the unit size 2337 // - unit: number of bytes for each element of the collection 2338 func decInferLen(clen, maxlen, unit int) int { 2339 // anecdotal testing showed increase in allocation with map length of 16. 2340 // We saw same typical alloc from 0-8, then a 20% increase at 16. 2341 // Thus, we set it to 8. 2342 const ( 2343 minLenIfUnset = 8 2344 maxMem = 256 * 1024 // 256Kb Memory 2345 ) 2346 2347 // handle when maxlen is not set i.e. <= 0 2348 2349 // clen==0: use 0 2350 // maxlen<=0, clen<0: use default 2351 // maxlen> 0, clen<0: use default 2352 // maxlen<=0, clen>0: infer maxlen, and cap on it 2353 // maxlen> 0, clen>0: cap at maxlen 2354 2355 if clen == 0 || clen == containerLenNil { 2356 return 0 2357 } 2358 if clen < 0 { 2359 // if unspecified, return 64 for bytes, ... 8 for uint64, ... and everything else 2360 clen = 64 / unit 2361 if clen > minLenIfUnset { 2362 return clen 2363 } 2364 return minLenIfUnset 2365 } 2366 if unit <= 0 { 2367 return clen 2368 } 2369 if maxlen <= 0 { 2370 maxlen = maxMem / unit 2371 } 2372 if clen < maxlen { 2373 return clen 2374 } 2375 return maxlen 2376 }