github.com/ugorji/go/codec@v1.2.13-0.20240307214044-07c54c229a5a/decode.go (about) 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) HandleName() string { 1403 return d.hh.Name() 1404 } 1405 1406 func (d *Decoder) r() *decRd { 1407 return &d.decRd 1408 } 1409 1410 func (d *Decoder) init(h Handle) { 1411 initHandle(h) 1412 d.cbreak = d.js || d.cbor 1413 d.bytes = true 1414 d.err = errDecoderNotInitialized 1415 d.h = h.getBasicHandle() 1416 d.hh = h 1417 d.be = h.isBinary() 1418 if d.h.InternString && d.is == nil { 1419 d.is.init() 1420 } 1421 // NOTE: do not initialize d.n here. It is lazily initialized in d.naked() 1422 } 1423 1424 func (d *Decoder) resetCommon() { 1425 d.d.reset() 1426 d.err = nil 1427 d.c = 0 1428 d.decByteState = decByteStateNone 1429 d.depth = 0 1430 d.calls = 0 1431 // reset all things which were cached from the Handle, but could change 1432 d.maxdepth = decDefMaxDepth 1433 if d.h.MaxDepth > 0 { 1434 d.maxdepth = d.h.MaxDepth 1435 } 1436 d.mtid = 0 1437 d.stid = 0 1438 d.mtr = false 1439 d.str = false 1440 if d.h.MapType != nil { 1441 d.mtid = rt2id(d.h.MapType) 1442 d.mtr = fastpathAvIndex(d.mtid) != -1 1443 } 1444 if d.h.SliceType != nil { 1445 d.stid = rt2id(d.h.SliceType) 1446 d.str = fastpathAvIndex(d.stid) != -1 1447 } 1448 } 1449 1450 // Reset the Decoder with a new Reader to decode from, 1451 // clearing all state from last run(s). 1452 func (d *Decoder) Reset(r io.Reader) { 1453 if r == nil { 1454 r = &eofReader 1455 } 1456 d.bytes = false 1457 if d.ri == nil { 1458 d.ri = new(ioDecReader) 1459 } 1460 d.ri.reset(r, d.h.ReaderBufferSize, &d.blist) 1461 d.decReader = d.ri 1462 d.resetCommon() 1463 } 1464 1465 // ResetBytes resets the Decoder with a new []byte to decode from, 1466 // clearing all state from last run(s). 1467 func (d *Decoder) ResetBytes(in []byte) { 1468 if in == nil { 1469 in = []byte{} 1470 } 1471 d.bytes = true 1472 d.decReader = &d.rb 1473 d.rb.reset(in) 1474 d.resetCommon() 1475 } 1476 1477 // ResetString resets the Decoder with a new string to decode from, 1478 // clearing all state from last run(s). 1479 // 1480 // It is a convenience function that calls ResetBytes with a 1481 // []byte view into the string. 1482 // 1483 // This can be an efficient zero-copy if using default mode i.e. without codec.safe tag. 1484 func (d *Decoder) ResetString(s string) { 1485 d.ResetBytes(bytesView(s)) 1486 } 1487 1488 func (d *Decoder) naked() *fauxUnion { 1489 return &d.n 1490 } 1491 1492 // Decode decodes the stream from reader and stores the result in the 1493 // value pointed to by v. v cannot be a nil pointer. v can also be 1494 // a reflect.Value of a pointer. 1495 // 1496 // Note that a pointer to a nil interface is not a nil pointer. 1497 // If you do not know what type of stream it is, pass in a pointer to a nil interface. 1498 // We will decode and store a value in that nil interface. 1499 // 1500 // Sample usages: 1501 // 1502 // // Decoding into a non-nil typed value 1503 // var f float32 1504 // err = codec.NewDecoder(r, handle).Decode(&f) 1505 // 1506 // // Decoding into nil interface 1507 // var v interface{} 1508 // dec := codec.NewDecoder(r, handle) 1509 // err = dec.Decode(&v) 1510 // 1511 // When decoding into a nil interface{}, we will decode into an appropriate value based 1512 // on the contents of the stream: 1513 // - Numbers are decoded as float64, int64 or uint64. 1514 // - Other values are decoded appropriately depending on the type: 1515 // bool, string, []byte, time.Time, etc 1516 // - Extensions are decoded as RawExt (if no ext function registered for the tag) 1517 // 1518 // Configurations exist on the Handle to override defaults 1519 // (e.g. for MapType, SliceType and how to decode raw bytes). 1520 // 1521 // When decoding into a non-nil interface{} value, the mode of encoding is based on the 1522 // type of the value. When a value is seen: 1523 // - If an extension is registered for it, call that extension function 1524 // - If it implements BinaryUnmarshaler, call its UnmarshalBinary(data []byte) error 1525 // - Else decode it based on its reflect.Kind 1526 // 1527 // There are some special rules when decoding into containers (slice/array/map/struct). 1528 // Decode will typically use the stream contents to UPDATE the container i.e. the values 1529 // in these containers will not be zero'ed before decoding. 1530 // - A map can be decoded from a stream map, by updating matching keys. 1531 // - A slice can be decoded from a stream array, 1532 // by updating the first n elements, where n is length of the stream. 1533 // - A slice can be decoded from a stream map, by decoding as if 1534 // it contains a sequence of key-value pairs. 1535 // - A struct can be decoded from a stream map, by updating matching fields. 1536 // - A struct can be decoded from a stream array, 1537 // by updating fields as they occur in the struct (by index). 1538 // 1539 // This in-place update maintains consistency in the decoding philosophy (i.e. we ALWAYS update 1540 // in place by default). However, the consequence of this is that values in slices or maps 1541 // which are not zero'ed before hand, will have part of the prior values in place after decode 1542 // if the stream doesn't contain an update for those parts. 1543 // 1544 // This in-place update can be disabled by configuring the MapValueReset and SliceElementReset 1545 // decode options available on every handle. 1546 // 1547 // Furthermore, when decoding a stream map or array with length of 0 into a nil map or slice, 1548 // we reset the destination map or slice to a zero-length value. 1549 // 1550 // However, when decoding a stream nil, we reset the destination container 1551 // to its "zero" value (e.g. nil for slice/map, etc). 1552 // 1553 // Note: we allow nil values in the stream anywhere except for map keys. 1554 // A nil value in the encoded stream where a map key is expected is treated as an error. 1555 func (d *Decoder) Decode(v interface{}) (err error) { 1556 // tried to use closure, as runtime optimizes defer with no params. 1557 // This seemed to be causing weird issues (like circular reference found, unexpected panic, etc). 1558 // Also, see https://github.com/golang/go/issues/14939#issuecomment-417836139 1559 if !debugging { 1560 defer func() { 1561 if x := recover(); x != nil { 1562 panicValToErr(d, x, &d.err) 1563 err = d.err 1564 } 1565 }() 1566 } 1567 1568 d.MustDecode(v) 1569 return 1570 } 1571 1572 // MustDecode is like Decode, but panics if unable to Decode. 1573 // 1574 // Note: This provides insight to the code location that triggered the error. 1575 func (d *Decoder) MustDecode(v interface{}) { 1576 halt.onerror(d.err) 1577 if d.hh == nil { 1578 halt.onerror(errNoFormatHandle) 1579 } 1580 1581 // Top-level: v is a pointer and not nil. 1582 d.calls++ 1583 d.decode(v) 1584 d.calls-- 1585 } 1586 1587 // Release is a no-op. 1588 // 1589 // Deprecated: Pooled resources are not used with a Decoder. 1590 // This method is kept for compatibility reasons only. 1591 func (d *Decoder) Release() { 1592 } 1593 1594 func (d *Decoder) swallow() { 1595 d.d.nextValueBytes(nil) 1596 } 1597 1598 func (d *Decoder) swallowErr() (err error) { 1599 if !debugging { 1600 defer func() { 1601 if x := recover(); x != nil { 1602 panicValToErr(d, x, &err) 1603 } 1604 }() 1605 } 1606 d.swallow() 1607 return 1608 } 1609 1610 func setZero(iv interface{}) { 1611 if iv == nil { 1612 return 1613 } 1614 rv, ok := isNil(iv) 1615 if ok { 1616 return 1617 } 1618 // var canDecode bool 1619 switch v := iv.(type) { 1620 case *string: 1621 *v = "" 1622 case *bool: 1623 *v = false 1624 case *int: 1625 *v = 0 1626 case *int8: 1627 *v = 0 1628 case *int16: 1629 *v = 0 1630 case *int32: 1631 *v = 0 1632 case *int64: 1633 *v = 0 1634 case *uint: 1635 *v = 0 1636 case *uint8: 1637 *v = 0 1638 case *uint16: 1639 *v = 0 1640 case *uint32: 1641 *v = 0 1642 case *uint64: 1643 *v = 0 1644 case *float32: 1645 *v = 0 1646 case *float64: 1647 *v = 0 1648 case *complex64: 1649 *v = 0 1650 case *complex128: 1651 *v = 0 1652 case *[]byte: 1653 *v = nil 1654 case *Raw: 1655 *v = nil 1656 case *time.Time: 1657 *v = time.Time{} 1658 case reflect.Value: 1659 decSetNonNilRV2Zero(v) 1660 default: 1661 if !fastpathDecodeSetZeroTypeSwitch(iv) { 1662 decSetNonNilRV2Zero(rv) 1663 } 1664 } 1665 } 1666 1667 // decSetNonNilRV2Zero will set the non-nil value to its zero value. 1668 func decSetNonNilRV2Zero(v reflect.Value) { 1669 // If not decodeable (settable), we do not touch it. 1670 // We considered empty'ing it if not decodeable e.g. 1671 // - if chan, drain it 1672 // - if map, clear it 1673 // - if slice or array, zero all elements up to len 1674 // 1675 // However, we decided instead that we either will set the 1676 // whole value to the zero value, or leave AS IS. 1677 1678 k := v.Kind() 1679 if k == reflect.Interface { 1680 decSetNonNilRV2Zero4Intf(v) 1681 } else if k == reflect.Ptr { 1682 decSetNonNilRV2Zero4Ptr(v) 1683 } else if v.CanSet() { 1684 rvSetDirectZero(v) 1685 } 1686 } 1687 1688 func decSetNonNilRV2Zero4Ptr(v reflect.Value) { 1689 ve := v.Elem() 1690 if ve.CanSet() { 1691 rvSetZero(ve) // we can have a pointer to an interface 1692 } else if v.CanSet() { 1693 rvSetZero(v) 1694 } 1695 } 1696 1697 func decSetNonNilRV2Zero4Intf(v reflect.Value) { 1698 ve := v.Elem() 1699 if ve.CanSet() { 1700 rvSetDirectZero(ve) // interfaces always have element as a non-interface 1701 } else if v.CanSet() { 1702 rvSetZero(v) 1703 } 1704 } 1705 1706 func (d *Decoder) decode(iv interface{}) { 1707 // a switch with only concrete types can be optimized. 1708 // consequently, we deal with nil and interfaces outside the switch. 1709 1710 if iv == nil { 1711 d.onerror(errCannotDecodeIntoNil) 1712 } 1713 1714 switch v := iv.(type) { 1715 // case nil: 1716 // case Selfer: 1717 case reflect.Value: 1718 if x, _ := isDecodeable(v); !x { 1719 d.haltAsNotDecodeable(v) 1720 } 1721 d.decodeValue(v, nil) 1722 case *string: 1723 *v = d.stringZC(d.d.DecodeStringAsBytes()) 1724 case *bool: 1725 *v = d.d.DecodeBool() 1726 case *int: 1727 *v = int(chkOvf.IntV(d.d.DecodeInt64(), intBitsize)) 1728 case *int8: 1729 *v = int8(chkOvf.IntV(d.d.DecodeInt64(), 8)) 1730 case *int16: 1731 *v = int16(chkOvf.IntV(d.d.DecodeInt64(), 16)) 1732 case *int32: 1733 *v = int32(chkOvf.IntV(d.d.DecodeInt64(), 32)) 1734 case *int64: 1735 *v = d.d.DecodeInt64() 1736 case *uint: 1737 *v = uint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize)) 1738 case *uint8: 1739 *v = uint8(chkOvf.UintV(d.d.DecodeUint64(), 8)) 1740 case *uint16: 1741 *v = uint16(chkOvf.UintV(d.d.DecodeUint64(), 16)) 1742 case *uint32: 1743 *v = uint32(chkOvf.UintV(d.d.DecodeUint64(), 32)) 1744 case *uint64: 1745 *v = d.d.DecodeUint64() 1746 case *float32: 1747 *v = d.decodeFloat32() 1748 case *float64: 1749 *v = d.d.DecodeFloat64() 1750 case *complex64: 1751 *v = complex(d.decodeFloat32(), 0) 1752 case *complex128: 1753 *v = complex(d.d.DecodeFloat64(), 0) 1754 case *[]byte: 1755 *v = d.decodeBytesInto(*v) 1756 case []byte: 1757 // not addressable byte slice, so do not decode into it past the length 1758 b := d.decodeBytesInto(v[:len(v):len(v)]) 1759 if !(len(b) > 0 && len(b) == len(v) && &b[0] == &v[0]) { // not same slice 1760 copy(v, b) 1761 } 1762 case *time.Time: 1763 *v = d.d.DecodeTime() 1764 case *Raw: 1765 *v = d.rawBytes() 1766 1767 case *interface{}: 1768 d.decodeValue(rv4iptr(v), nil) 1769 1770 default: 1771 // we can't check non-predefined types, as they might be a Selfer or extension. 1772 if skipFastpathTypeSwitchInDirectCall || !fastpathDecodeTypeSwitch(iv, d) { 1773 v := reflect.ValueOf(iv) 1774 if x, _ := isDecodeable(v); !x { 1775 d.haltAsNotDecodeable(v) 1776 } 1777 d.decodeValue(v, nil) 1778 } 1779 } 1780 } 1781 1782 // decodeValue MUST be called by the actual value we want to decode into, 1783 // not its addr or a reference to it. 1784 // 1785 // This way, we know if it is itself a pointer, and can handle nil in 1786 // the stream effectively. 1787 // 1788 // Note that decodeValue will handle nil in the stream early, so that the 1789 // subsequent calls i.e. kXXX methods, etc do not have to handle it themselves. 1790 func (d *Decoder) decodeValue(rv reflect.Value, fn *codecFn) { 1791 if d.d.TryNil() { 1792 decSetNonNilRV2Zero(rv) 1793 return 1794 } 1795 d.decodeValueNoCheckNil(rv, fn) 1796 } 1797 1798 func (d *Decoder) decodeValueNoCheckNil(rv reflect.Value, fn *codecFn) { 1799 // If stream is not containing a nil value, then we can deref to the base 1800 // non-pointer value, and decode into that. 1801 var rvp reflect.Value 1802 var rvpValid bool 1803 PTR: 1804 if rv.Kind() == reflect.Ptr { 1805 rvpValid = true 1806 if rvIsNil(rv) { 1807 rvSetDirect(rv, reflect.New(rv.Type().Elem())) 1808 } 1809 rvp = rv 1810 rv = rv.Elem() 1811 goto PTR 1812 } 1813 1814 if fn == nil { 1815 fn = d.h.fn(rv.Type()) 1816 } 1817 if fn.i.addrD { 1818 if rvpValid { 1819 rv = rvp 1820 } else if rv.CanAddr() { 1821 rv = rvAddr(rv, fn.i.ti.ptr) 1822 } else if fn.i.addrDf { 1823 d.errorf("cannot decode into a non-pointer value") 1824 } 1825 } 1826 fn.fd(d, &fn.i, rv) 1827 } 1828 1829 func (d *Decoder) structFieldNotFound(index int, rvkencname string) { 1830 // Note: rvkencname is used only if there is an error, to pass into d.errorf. 1831 // Consequently, it is ok to pass in a stringView 1832 // Since rvkencname may be a stringView, do NOT pass it to another function. 1833 if d.h.ErrorIfNoField { 1834 if index >= 0 { 1835 d.errorf("no matching struct field found when decoding stream array at index %v", index) 1836 } else if rvkencname != "" { 1837 d.errorf("no matching struct field found when decoding stream map with key " + rvkencname) 1838 } 1839 } 1840 d.swallow() 1841 } 1842 1843 func (d *Decoder) arrayCannotExpand(sliceLen, streamLen int) { 1844 if d.h.ErrorIfNoArrayExpand { 1845 d.errorf("cannot expand array len during decode from %v to %v", sliceLen, streamLen) 1846 } 1847 } 1848 1849 func (d *Decoder) haltAsNotDecodeable(rv reflect.Value) { 1850 if !rv.IsValid() { 1851 d.onerror(errCannotDecodeIntoNil) 1852 } 1853 // check if an interface can be retrieved, before grabbing an interface 1854 if !rv.CanInterface() { 1855 d.errorf("cannot decode into a value without an interface: %v", rv) 1856 } 1857 d.errorf("cannot decode into value of kind: %v, %#v", rv.Kind(), rv2i(rv)) 1858 } 1859 1860 func (d *Decoder) depthIncr() { 1861 d.depth++ 1862 if d.depth >= d.maxdepth { 1863 d.onerror(errMaxDepthExceeded) 1864 } 1865 } 1866 1867 func (d *Decoder) depthDecr() { 1868 d.depth-- 1869 } 1870 1871 // Possibly get an interned version of a string, iff InternString=true and decoding a map key. 1872 // 1873 // This should mostly be used for map keys, where the key type is string. 1874 // This is because keys of a map/struct are typically reused across many objects. 1875 func (d *Decoder) string(v []byte) (s string) { 1876 if d.is == nil || d.c != containerMapKey || len(v) < 2 || len(v) > internMaxStrLen { 1877 return string(v) 1878 } 1879 return d.is.string(v) 1880 } 1881 1882 func (d *Decoder) zerocopy() bool { 1883 return d.bytes && d.h.ZeroCopy 1884 } 1885 1886 // decodeBytesInto is a convenience delegate function to decDriver.DecodeBytes. 1887 // It ensures that `in` is not a nil byte, before calling decDriver.DecodeBytes, 1888 // as decDriver.DecodeBytes treats a nil as a hint to use its internal scratch buffer. 1889 func (d *Decoder) decodeBytesInto(in []byte) (v []byte) { 1890 if in == nil { 1891 in = []byte{} 1892 } 1893 return d.d.DecodeBytes(in) 1894 } 1895 1896 func (d *Decoder) rawBytes() (v []byte) { 1897 // ensure that this is not a view into the bytes 1898 // i.e. if necessary, make new copy always. 1899 v = d.d.nextValueBytes([]byte{}) 1900 if d.bytes && !d.h.ZeroCopy { 1901 vv := make([]byte, len(v)) 1902 copy(vv, v) // using copy here triggers make+copy optimization eliding memclr 1903 v = vv 1904 } 1905 return 1906 } 1907 1908 func (d *Decoder) wrapErr(v error, err *error) { 1909 *err = wrapCodecErr(v, d.hh.Name(), d.NumBytesRead(), false) 1910 } 1911 1912 // NumBytesRead returns the number of bytes read 1913 func (d *Decoder) NumBytesRead() int { 1914 return int(d.r().numread()) 1915 } 1916 1917 // decodeFloat32 will delegate to an appropriate DecodeFloat32 implementation (if exists), 1918 // else if will call DecodeFloat64 and ensure the value doesn't overflow. 1919 // 1920 // Note that we return float64 to reduce unnecessary conversions 1921 func (d *Decoder) decodeFloat32() float32 { 1922 if d.js { 1923 return d.jsondriver().DecodeFloat32() // custom implementation for 32-bit 1924 } 1925 return float32(chkOvf.Float32V(d.d.DecodeFloat64())) 1926 } 1927 1928 // ---- container tracking 1929 // Note: We update the .c after calling the callback. 1930 // This way, the callback can know what the last status was. 1931 1932 // MARKER: do not call mapEnd if mapStart returns containerLenNil. 1933 1934 // MARKER: optimize decoding since all formats do not truly support all decDriver'ish operations. 1935 // - Read(Map|Array)Start is only supported by all formats. 1936 // - CheckBreak is only supported by json and cbor. 1937 // - Read(Map|Array)End is only supported by json. 1938 // - Read(Map|Array)Elem(Kay|Value) is only supported by json. 1939 // Honor these in the code, to reduce the number of interface calls (even if empty). 1940 1941 func (d *Decoder) checkBreak() (v bool) { 1942 // MARKER: jsonDecDriver.CheckBreak() cannot be inlined (over budget inlining cost). 1943 // Consequently, there's no benefit in incurring the cost of this wrapping function. 1944 // It is faster to just call the interface method directly. 1945 1946 // if d.js { 1947 // return d.jsondriver().CheckBreak() 1948 // } 1949 // if d.cbor { 1950 // return d.cbordriver().CheckBreak() 1951 // } 1952 1953 if d.cbreak { 1954 v = d.d.CheckBreak() 1955 } 1956 return 1957 } 1958 1959 func (d *Decoder) containerNext(j, containerLen int, hasLen bool) bool { 1960 // MARKER: keep in sync with gen-helper.go.tmpl 1961 1962 // return (hasLen && j < containerLen) || !(hasLen || slh.d.checkBreak()) 1963 if hasLen { 1964 return j < containerLen 1965 } 1966 return !d.checkBreak() 1967 } 1968 1969 func (d *Decoder) mapStart(v int) int { 1970 if v != containerLenNil { 1971 d.depthIncr() 1972 d.c = containerMapStart 1973 } 1974 return v 1975 } 1976 1977 func (d *Decoder) mapElemKey() { 1978 if d.js { 1979 d.jsondriver().ReadMapElemKey() 1980 } 1981 d.c = containerMapKey 1982 } 1983 1984 func (d *Decoder) mapElemValue() { 1985 if d.js { 1986 d.jsondriver().ReadMapElemValue() 1987 } 1988 d.c = containerMapValue 1989 } 1990 1991 func (d *Decoder) mapEnd() { 1992 if d.js { 1993 d.jsondriver().ReadMapEnd() 1994 } 1995 // d.d.ReadMapEnd() 1996 d.depthDecr() 1997 d.c = 0 1998 } 1999 2000 func (d *Decoder) arrayStart(v int) int { 2001 if v != containerLenNil { 2002 d.depthIncr() 2003 d.c = containerArrayStart 2004 } 2005 return v 2006 } 2007 2008 func (d *Decoder) arrayElem() { 2009 if d.js { 2010 d.jsondriver().ReadArrayElem() 2011 } 2012 d.c = containerArrayElem 2013 } 2014 2015 func (d *Decoder) arrayEnd() { 2016 if d.js { 2017 d.jsondriver().ReadArrayEnd() 2018 } 2019 // d.d.ReadArrayEnd() 2020 d.depthDecr() 2021 d.c = 0 2022 } 2023 2024 func (d *Decoder) interfaceExtConvertAndDecode(v interface{}, ext InterfaceExt) { 2025 // var v interface{} = ext.ConvertExt(rv) 2026 // d.d.decode(&v) 2027 // ext.UpdateExt(rv, v) 2028 2029 // assume v is a pointer: 2030 // - if struct|array, pass as is to ConvertExt 2031 // - else make it non-addressable and pass to ConvertExt 2032 // - make return value from ConvertExt addressable 2033 // - decode into it 2034 // - return the interface for passing into UpdateExt. 2035 // - interface should be a pointer if struct|array, else a value 2036 2037 var s interface{} 2038 rv := reflect.ValueOf(v) 2039 rv2 := rv.Elem() 2040 rvk := rv2.Kind() 2041 if rvk == reflect.Struct || rvk == reflect.Array { 2042 s = ext.ConvertExt(v) 2043 } else { 2044 s = ext.ConvertExt(rv2i(rv2)) 2045 } 2046 rv = reflect.ValueOf(s) 2047 2048 // We cannot use isDecodeable here, as the value converted may be nil, 2049 // or it may not be nil but is not addressable and thus we cannot extend it, etc. 2050 // Instead, we just ensure that the value is addressable. 2051 2052 if !rv.CanAddr() { 2053 rvk = rv.Kind() 2054 rv2 = d.oneShotAddrRV(rv.Type(), rvk) 2055 if rvk == reflect.Interface { 2056 rvSetIntf(rv2, rv) 2057 } else { 2058 rvSetDirect(rv2, rv) 2059 } 2060 rv = rv2 2061 } 2062 2063 d.decodeValue(rv, nil) 2064 ext.UpdateExt(v, rv2i(rv)) 2065 } 2066 2067 func (d *Decoder) sideDecode(v interface{}, basetype reflect.Type, bs []byte) { 2068 // NewDecoderBytes(bs, d.hh).decodeValue(baseRV(v), d.h.fnNoExt(basetype)) 2069 2070 defer func(rb bytesDecReader, bytes bool, 2071 c containerState, dbs decByteState, depth int16, r decReader, state interface{}) { 2072 d.rb = rb 2073 d.bytes = bytes 2074 d.c = c 2075 d.decByteState = dbs 2076 d.depth = depth 2077 d.decReader = r 2078 d.d.restoreState(state) 2079 }(d.rb, d.bytes, d.c, d.decByteState, d.depth, d.decReader, d.d.captureState()) 2080 2081 // d.rb.reset(in) 2082 d.rb = bytesDecReader{bs[:len(bs):len(bs)], 0} 2083 d.bytes = true 2084 d.decReader = &d.rb 2085 d.d.resetState() 2086 d.c = 0 2087 d.decByteState = decByteStateNone 2088 d.depth = 0 2089 2090 // must call using fnNoExt 2091 d.decodeValue(baseRV(v), d.h.fnNoExt(basetype)) 2092 } 2093 2094 func (d *Decoder) fauxUnionReadRawBytes(asString bool) { 2095 if asString || d.h.RawToString { 2096 d.n.v = valueTypeString 2097 // fauxUnion is only used within DecodeNaked calls; consequently, we should try to intern. 2098 d.n.s = d.stringZC(d.d.DecodeBytes(nil)) 2099 } else { 2100 d.n.v = valueTypeBytes 2101 d.n.l = d.d.DecodeBytes([]byte{}) 2102 } 2103 } 2104 2105 func (d *Decoder) oneShotAddrRV(rvt reflect.Type, rvk reflect.Kind) reflect.Value { 2106 if decUseTransient && 2107 (numBoolStrSliceBitset.isset(byte(rvk)) || 2108 ((rvk == reflect.Struct || rvk == reflect.Array) && 2109 d.h.getTypeInfo(rt2id(rvt), rvt).flagCanTransient)) { 2110 return d.perType.TransientAddrK(rvt, rvk) 2111 } 2112 return rvZeroAddrK(rvt, rvk) 2113 } 2114 2115 // -------------------------------------------------- 2116 2117 // decSliceHelper assists when decoding into a slice, from a map or an array in the stream. 2118 // A slice can be set from a map or array in stream. This supports the MapBySlice interface. 2119 // 2120 // Note: if IsNil, do not call ElemContainerState. 2121 type decSliceHelper struct { 2122 d *Decoder 2123 ct valueType 2124 Array bool 2125 IsNil bool 2126 } 2127 2128 func (d *Decoder) decSliceHelperStart() (x decSliceHelper, clen int) { 2129 x.ct = d.d.ContainerType() 2130 x.d = d 2131 switch x.ct { 2132 case valueTypeNil: 2133 x.IsNil = true 2134 case valueTypeArray: 2135 x.Array = true 2136 clen = d.arrayStart(d.d.ReadArrayStart()) 2137 case valueTypeMap: 2138 clen = d.mapStart(d.d.ReadMapStart()) 2139 clen += clen 2140 default: 2141 d.errorf("only encoded map or array can be decoded into a slice (%d)", x.ct) 2142 } 2143 return 2144 } 2145 2146 func (x decSliceHelper) End() { 2147 if x.IsNil { 2148 } else if x.Array { 2149 x.d.arrayEnd() 2150 } else { 2151 x.d.mapEnd() 2152 } 2153 } 2154 2155 func (x decSliceHelper) ElemContainerState(index int) { 2156 // Note: if isnil, clen=0, so we never call into ElemContainerState 2157 2158 if x.Array { 2159 x.d.arrayElem() 2160 } else if index&1 == 0 { // index%2 == 0 { 2161 x.d.mapElemKey() 2162 } else { 2163 x.d.mapElemValue() 2164 } 2165 } 2166 2167 func (x decSliceHelper) arrayCannotExpand(hasLen bool, lenv, j, containerLenS int) { 2168 x.d.arrayCannotExpand(lenv, j+1) 2169 // drain completely and return 2170 x.ElemContainerState(j) 2171 x.d.swallow() 2172 j++ 2173 for ; x.d.containerNext(j, containerLenS, hasLen); j++ { 2174 x.ElemContainerState(j) 2175 x.d.swallow() 2176 } 2177 x.End() 2178 } 2179 2180 // decNextValueBytesHelper helps with NextValueBytes calls. 2181 // 2182 // Typical usage: 2183 // - each Handle's decDriver will implement a high level nextValueBytes, 2184 // which will track the current cursor, delegate to a nextValueBytesR 2185 // method, and then potentially call bytesRdV at the end. 2186 // 2187 // See simple.go for typical usage model. 2188 type decNextValueBytesHelper struct { 2189 d *Decoder 2190 } 2191 2192 func (x decNextValueBytesHelper) append1(v *[]byte, b byte) { 2193 if *v != nil && !x.d.bytes { 2194 *v = append(*v, b) 2195 } 2196 } 2197 2198 func (x decNextValueBytesHelper) appendN(v *[]byte, b ...byte) { 2199 if *v != nil && !x.d.bytes { 2200 *v = append(*v, b...) 2201 } 2202 } 2203 2204 func (x decNextValueBytesHelper) appendS(v *[]byte, b string) { 2205 if *v != nil && !x.d.bytes { 2206 *v = append(*v, b...) 2207 } 2208 } 2209 2210 func (x decNextValueBytesHelper) bytesRdV(v *[]byte, startpos uint) { 2211 if x.d.bytes { 2212 *v = x.d.rb.b[startpos:x.d.rb.c] 2213 } 2214 } 2215 2216 // decNegintPosintFloatNumberHelper is used for formats that are binary 2217 // and have distinct ways of storing positive integers vs negative integers 2218 // vs floats, which are uniquely identified by the byte descriptor. 2219 // 2220 // Currently, these formats are binc, cbor and simple. 2221 type decNegintPosintFloatNumberHelper struct { 2222 d *Decoder 2223 } 2224 2225 func (x decNegintPosintFloatNumberHelper) uint64(ui uint64, neg, ok bool) uint64 { 2226 if ok && !neg { 2227 return ui 2228 } 2229 return x.uint64TryFloat(ok) 2230 } 2231 2232 func (x decNegintPosintFloatNumberHelper) uint64TryFloat(ok bool) (ui uint64) { 2233 if ok { // neg = true 2234 x.d.errorf("assigning negative signed value to unsigned type") 2235 } 2236 f, ok := x.d.d.decFloat() 2237 if ok && f >= 0 && noFrac64(math.Float64bits(f)) { 2238 ui = uint64(f) 2239 } else { 2240 x.d.errorf("invalid number loading uint64, with descriptor: %v", x.d.d.descBd()) 2241 } 2242 return ui 2243 } 2244 2245 func decNegintPosintFloatNumberHelperInt64v(ui uint64, neg, incrIfNeg bool) (i int64) { 2246 if neg && incrIfNeg { 2247 ui++ 2248 } 2249 i = chkOvf.SignedIntV(ui) 2250 if neg { 2251 i = -i 2252 } 2253 return 2254 } 2255 2256 func (x decNegintPosintFloatNumberHelper) int64(ui uint64, neg, ok bool) (i int64) { 2257 if ok { 2258 return decNegintPosintFloatNumberHelperInt64v(ui, neg, x.d.cbor) 2259 } 2260 // return x.int64TryFloat() 2261 // } 2262 // func (x decNegintPosintFloatNumberHelper) int64TryFloat() (i int64) { 2263 f, ok := x.d.d.decFloat() 2264 if ok && noFrac64(math.Float64bits(f)) { 2265 i = int64(f) 2266 } else { 2267 x.d.errorf("invalid number loading uint64, with descriptor: %v", x.d.d.descBd()) 2268 } 2269 return 2270 } 2271 2272 func (x decNegintPosintFloatNumberHelper) float64(f float64, ok bool) float64 { 2273 if ok { 2274 return f 2275 } 2276 return x.float64TryInteger() 2277 } 2278 2279 func (x decNegintPosintFloatNumberHelper) float64TryInteger() float64 { 2280 ui, neg, ok := x.d.d.decInteger() 2281 if !ok { 2282 x.d.errorf("invalid descriptor for float: %v", x.d.d.descBd()) 2283 } 2284 return float64(decNegintPosintFloatNumberHelperInt64v(ui, neg, x.d.cbor)) 2285 } 2286 2287 // isDecodeable checks if value can be decoded into 2288 // 2289 // decode can take any reflect.Value that is a inherently addressable i.e. 2290 // - non-nil chan (we will SEND to it) 2291 // - non-nil slice (we will set its elements) 2292 // - non-nil map (we will put into it) 2293 // - non-nil pointer (we can "update" it) 2294 // - func: no 2295 // - interface: no 2296 // - array: if canAddr=true 2297 // - any other value pointer: if canAddr=true 2298 func isDecodeable(rv reflect.Value) (canDecode bool, reason decNotDecodeableReason) { 2299 switch rv.Kind() { 2300 case reflect.Ptr, reflect.Slice, reflect.Chan, reflect.Map: 2301 canDecode = !rvIsNil(rv) 2302 reason = decNotDecodeableReasonNilReference 2303 case reflect.Func, reflect.Interface, reflect.Invalid, reflect.UnsafePointer: 2304 reason = decNotDecodeableReasonBadKind 2305 default: 2306 canDecode = rv.CanAddr() 2307 reason = decNotDecodeableReasonNonAddrValue 2308 } 2309 return 2310 } 2311 2312 func decByteSlice(r *decRd, clen, maxInitLen int, bs []byte) (bsOut []byte) { 2313 if clen <= 0 { 2314 bsOut = zeroByteSlice 2315 } else if cap(bs) >= clen { 2316 bsOut = bs[:clen] 2317 r.readb(bsOut) 2318 } else { 2319 var len2 int 2320 for len2 < clen { 2321 len3 := decInferLen(clen-len2, maxInitLen, 1) 2322 bs3 := bsOut 2323 bsOut = make([]byte, len2+len3) 2324 copy(bsOut, bs3) 2325 r.readb(bsOut[len2:]) 2326 len2 += len3 2327 } 2328 } 2329 return 2330 } 2331 2332 // decInferLen will infer a sensible length, given the following: 2333 // - clen: length wanted. 2334 // - maxlen: max length to be returned. 2335 // if <= 0, it is unset, and we infer it based on the unit size 2336 // - unit: number of bytes for each element of the collection 2337 func decInferLen(clen, maxlen, unit int) int { 2338 // anecdotal testing showed increase in allocation with map length of 16. 2339 // We saw same typical alloc from 0-8, then a 20% increase at 16. 2340 // Thus, we set it to 8. 2341 const ( 2342 minLenIfUnset = 8 2343 maxMem = 256 * 1024 // 256Kb Memory 2344 ) 2345 2346 // handle when maxlen is not set i.e. <= 0 2347 2348 // clen==0: use 0 2349 // maxlen<=0, clen<0: use default 2350 // maxlen> 0, clen<0: use default 2351 // maxlen<=0, clen>0: infer maxlen, and cap on it 2352 // maxlen> 0, clen>0: cap at maxlen 2353 2354 if clen == 0 || clen == containerLenNil { 2355 return 0 2356 } 2357 if clen < 0 { 2358 // if unspecified, return 64 for bytes, ... 8 for uint64, ... and everything else 2359 clen = 64 / unit 2360 if clen > minLenIfUnset { 2361 return clen 2362 } 2363 return minLenIfUnset 2364 } 2365 if unit <= 0 { 2366 return clen 2367 } 2368 if maxlen <= 0 { 2369 maxlen = maxMem / unit 2370 } 2371 if clen < maxlen { 2372 return clen 2373 } 2374 return maxlen 2375 }