github.com/ugorji/go/codec@v1.2.13-0.20240307214044-07c54c229a5a/helper_not_unsafe.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 //go:build !go1.9 || safe || codec.safe || appengine 5 // +build !go1.9 safe codec.safe appengine 6 7 package codec 8 9 import ( 10 // "hash/adler32" 11 "math" 12 "reflect" 13 "sync/atomic" 14 "time" 15 ) 16 17 // This file has safe variants of some helper functions. 18 // MARKER: See helper_unsafe.go for the usage documentation. 19 20 const safeMode = true 21 22 const transientSizeMax = 0 23 const transientValueHasStringSlice = true 24 25 func byteAt(b []byte, index uint) byte { 26 return b[index] 27 } 28 29 func setByteAt(b []byte, index uint, val byte) { 30 b[index] = val 31 } 32 33 func byteSliceOf(b []byte, start, end uint) []byte { 34 return b[start:end] 35 } 36 37 // func byteSliceWithLen(b []byte, length uint) []byte { 38 // return b[:length] 39 // } 40 41 func stringView(v []byte) string { 42 return string(v) 43 } 44 45 func bytesView(v string) []byte { 46 return []byte(v) 47 } 48 49 func byteSliceSameData(v1 []byte, v2 []byte) bool { 50 return cap(v1) != 0 && cap(v2) != 0 && &(v1[:1][0]) == &(v2[:1][0]) 51 } 52 53 func okBytes2(b []byte) (v [2]byte) { 54 copy(v[:], b) 55 return 56 } 57 58 func okBytes3(b []byte) (v [3]byte) { 59 copy(v[:], b) 60 return 61 } 62 63 func okBytes4(b []byte) (v [4]byte) { 64 copy(v[:], b) 65 return 66 } 67 68 func okBytes8(b []byte) (v [8]byte) { 69 copy(v[:], b) 70 return 71 } 72 73 func isNil(v interface{}) (rv reflect.Value, isnil bool) { 74 rv = reflect.ValueOf(v) 75 if isnilBitset.isset(byte(rv.Kind())) { 76 isnil = rv.IsNil() 77 } 78 return 79 } 80 81 func eq4i(i0, i1 interface{}) bool { 82 return i0 == i1 83 } 84 85 func rv4iptr(i interface{}) reflect.Value { return reflect.ValueOf(i) } 86 func rv4istr(i interface{}) reflect.Value { return reflect.ValueOf(i) } 87 88 // func rv4i(i interface{}) reflect.Value { return reflect.ValueOf(i) } 89 // func rv4iK(i interface{}, kind byte, isref bool) reflect.Value { return reflect.ValueOf(i) } 90 91 func rv2i(rv reflect.Value) interface{} { 92 return rv.Interface() 93 } 94 95 func rvAddr(rv reflect.Value, ptrType reflect.Type) reflect.Value { 96 return rv.Addr() 97 } 98 99 func rvIsNil(rv reflect.Value) bool { 100 return rv.IsNil() 101 } 102 103 func rvSetSliceLen(rv reflect.Value, length int) { 104 rv.SetLen(length) 105 } 106 107 func rvZeroAddrK(t reflect.Type, k reflect.Kind) reflect.Value { 108 return reflect.New(t).Elem() 109 } 110 111 func rvZeroK(t reflect.Type, k reflect.Kind) reflect.Value { 112 return reflect.Zero(t) 113 } 114 115 func rvConvert(v reflect.Value, t reflect.Type) (rv reflect.Value) { 116 // Note that reflect.Value.Convert(...) will make a copy if it is addressable. 117 // Since we decode into the passed value, we must try to convert the addressable value.. 118 if v.CanAddr() { 119 return v.Addr().Convert(reflect.PtrTo(t)).Elem() 120 } 121 return v.Convert(t) 122 } 123 124 func rt2id(rt reflect.Type) uintptr { 125 return reflect.ValueOf(rt).Pointer() 126 } 127 128 func i2rtid(i interface{}) uintptr { 129 return reflect.ValueOf(reflect.TypeOf(i)).Pointer() 130 } 131 132 // -------------------------- 133 134 func isEmptyValue(v reflect.Value, tinfos *TypeInfos, recursive bool) bool { 135 switch v.Kind() { 136 case reflect.Invalid: 137 return true 138 case reflect.String: 139 return v.Len() == 0 140 case reflect.Array: 141 // zero := reflect.Zero(v.Type().Elem()) 142 // can I just check if the whole value is equal to zeros? seems not. 143 // can I just check if the whole value is equal to its zero value? no. 144 // Well, then we check if each value is empty without recursive. 145 for i, vlen := 0, v.Len(); i < vlen; i++ { 146 if !isEmptyValue(v.Index(i), tinfos, false) { 147 return false 148 } 149 } 150 return true 151 case reflect.Map, reflect.Slice, reflect.Chan: 152 return v.IsNil() || v.Len() == 0 153 case reflect.Bool: 154 return !v.Bool() 155 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: 156 return v.Int() == 0 157 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr: 158 return v.Uint() == 0 159 case reflect.Complex64, reflect.Complex128: 160 c := v.Complex() 161 return math.Float64bits(real(c)) == 0 && math.Float64bits(imag(c)) == 0 162 case reflect.Float32, reflect.Float64: 163 return v.Float() == 0 164 case reflect.Func, reflect.UnsafePointer: 165 return v.IsNil() 166 case reflect.Interface, reflect.Ptr: 167 isnil := v.IsNil() 168 if recursive && !isnil { 169 return isEmptyValue(v.Elem(), tinfos, recursive) 170 } 171 return isnil 172 case reflect.Struct: 173 return isEmptyStruct(v, tinfos, recursive) 174 } 175 return false 176 } 177 178 // isEmptyStruct is only called from isEmptyValue, and checks if a struct is empty: 179 // - does it implement IsZero() bool 180 // - is it comparable, and can i compare directly using == 181 // - if checkStruct, then walk through the encodable fields 182 // and check if they are empty or not. 183 func isEmptyStruct(v reflect.Value, tinfos *TypeInfos, recursive bool) bool { 184 // v is a struct kind - no need to check again. 185 // We only check isZero on a struct kind, to reduce the amount of times 186 // that we lookup the rtid and typeInfo for each type as we walk the tree. 187 188 vt := v.Type() 189 rtid := rt2id(vt) 190 if tinfos == nil { 191 tinfos = defTypeInfos 192 } 193 ti := tinfos.get(rtid, vt) 194 if ti.rtid == timeTypId { 195 return rv2i(v).(time.Time).IsZero() 196 } 197 if ti.flagIsZeroer { 198 return rv2i(v).(isZeroer).IsZero() 199 } 200 if ti.flagIsZeroerPtr && v.CanAddr() { 201 return rv2i(v.Addr()).(isZeroer).IsZero() 202 } 203 if ti.flagIsCodecEmptyer { 204 return rv2i(v).(isCodecEmptyer).IsCodecEmpty() 205 } 206 if ti.flagIsCodecEmptyerPtr && v.CanAddr() { 207 return rv2i(v.Addr()).(isCodecEmptyer).IsCodecEmpty() 208 } 209 if ti.flagComparable { 210 return rv2i(v) == rv2i(rvZeroK(vt, reflect.Struct)) 211 } 212 if !recursive { 213 return false 214 } 215 // We only care about what we can encode/decode, 216 // so that is what we use to check omitEmpty. 217 for _, si := range ti.sfi.source() { 218 sfv := si.path.field(v) 219 if sfv.IsValid() && !isEmptyValue(sfv, tinfos, recursive) { 220 return false 221 } 222 } 223 return true 224 } 225 226 // -------------------------- 227 228 type perTypeElem struct { 229 t reflect.Type 230 rtid uintptr 231 zero reflect.Value 232 addr [2]reflect.Value 233 } 234 235 func (x *perTypeElem) get(index uint8) (v reflect.Value) { 236 v = x.addr[index%2] 237 if v.IsValid() { 238 v.Set(x.zero) 239 } else { 240 v = reflect.New(x.t).Elem() 241 x.addr[index%2] = v 242 } 243 return 244 } 245 246 type perType struct { 247 v []perTypeElem 248 } 249 250 type decPerType struct { 251 perType 252 } 253 254 type encPerType struct { 255 perType 256 } 257 258 func (x *perType) elem(t reflect.Type) *perTypeElem { 259 rtid := rt2id(t) 260 var h, i uint 261 var j = uint(len(x.v)) 262 LOOP: 263 if i < j { 264 h = (i + j) >> 1 // avoid overflow when computing h // h = i + (j-i)/2 265 if x.v[h].rtid < rtid { 266 i = h + 1 267 } else { 268 j = h 269 } 270 goto LOOP 271 } 272 if i < uint(len(x.v)) { 273 if x.v[i].rtid != rtid { 274 x.v = append(x.v, perTypeElem{}) 275 copy(x.v[i+1:], x.v[i:]) 276 x.v[i] = perTypeElem{t: t, rtid: rtid, zero: reflect.Zero(t)} 277 } 278 } else { 279 x.v = append(x.v, perTypeElem{t: t, rtid: rtid, zero: reflect.Zero(t)}) 280 } 281 return &x.v[i] 282 } 283 284 func (x *perType) TransientAddrK(t reflect.Type, k reflect.Kind) (rv reflect.Value) { 285 return x.elem(t).get(0) 286 } 287 288 func (x *perType) TransientAddr2K(t reflect.Type, k reflect.Kind) (rv reflect.Value) { 289 return x.elem(t).get(1) 290 } 291 292 func (x *perType) AddressableRO(v reflect.Value) (rv reflect.Value) { 293 rv = x.elem(v.Type()).get(0) 294 rvSetDirect(rv, v) 295 return 296 } 297 298 // -------------------------- 299 type structFieldInfos struct { 300 c []*structFieldInfo 301 s []*structFieldInfo 302 } 303 304 func (x *structFieldInfos) load(source, sorted []*structFieldInfo) { 305 x.c = source 306 x.s = sorted 307 } 308 309 func (x *structFieldInfos) sorted() (v []*structFieldInfo) { return x.s } 310 func (x *structFieldInfos) source() (v []*structFieldInfo) { return x.c } 311 312 type atomicClsErr struct { 313 v atomic.Value 314 } 315 316 func (x *atomicClsErr) load() (e clsErr) { 317 if i := x.v.Load(); i != nil { 318 e = i.(clsErr) 319 } 320 return 321 } 322 323 func (x *atomicClsErr) store(p clsErr) { 324 x.v.Store(p) 325 } 326 327 // -------------------------- 328 type atomicTypeInfoSlice struct { 329 v atomic.Value 330 } 331 332 func (x *atomicTypeInfoSlice) load() (e []rtid2ti) { 333 if i := x.v.Load(); i != nil { 334 e = i.([]rtid2ti) 335 } 336 return 337 } 338 339 func (x *atomicTypeInfoSlice) store(p []rtid2ti) { 340 x.v.Store(p) 341 } 342 343 // -------------------------- 344 type atomicRtidFnSlice struct { 345 v atomic.Value 346 } 347 348 func (x *atomicRtidFnSlice) load() (e []codecRtidFn) { 349 if i := x.v.Load(); i != nil { 350 e = i.([]codecRtidFn) 351 } 352 return 353 } 354 355 func (x *atomicRtidFnSlice) store(p []codecRtidFn) { 356 x.v.Store(p) 357 } 358 359 // -------------------------- 360 func (n *fauxUnion) ru() reflect.Value { 361 return reflect.ValueOf(&n.u).Elem() 362 } 363 func (n *fauxUnion) ri() reflect.Value { 364 return reflect.ValueOf(&n.i).Elem() 365 } 366 func (n *fauxUnion) rf() reflect.Value { 367 return reflect.ValueOf(&n.f).Elem() 368 } 369 func (n *fauxUnion) rl() reflect.Value { 370 return reflect.ValueOf(&n.l).Elem() 371 } 372 func (n *fauxUnion) rs() reflect.Value { 373 return reflect.ValueOf(&n.s).Elem() 374 } 375 func (n *fauxUnion) rt() reflect.Value { 376 return reflect.ValueOf(&n.t).Elem() 377 } 378 func (n *fauxUnion) rb() reflect.Value { 379 return reflect.ValueOf(&n.b).Elem() 380 } 381 382 // -------------------------- 383 func rvSetBytes(rv reflect.Value, v []byte) { 384 rv.SetBytes(v) 385 } 386 387 func rvSetString(rv reflect.Value, v string) { 388 rv.SetString(v) 389 } 390 391 func rvSetBool(rv reflect.Value, v bool) { 392 rv.SetBool(v) 393 } 394 395 func rvSetTime(rv reflect.Value, v time.Time) { 396 rv.Set(reflect.ValueOf(v)) 397 } 398 399 func rvSetFloat32(rv reflect.Value, v float32) { 400 rv.SetFloat(float64(v)) 401 } 402 403 func rvSetFloat64(rv reflect.Value, v float64) { 404 rv.SetFloat(v) 405 } 406 407 func rvSetComplex64(rv reflect.Value, v complex64) { 408 rv.SetComplex(complex128(v)) 409 } 410 411 func rvSetComplex128(rv reflect.Value, v complex128) { 412 rv.SetComplex(v) 413 } 414 415 func rvSetInt(rv reflect.Value, v int) { 416 rv.SetInt(int64(v)) 417 } 418 419 func rvSetInt8(rv reflect.Value, v int8) { 420 rv.SetInt(int64(v)) 421 } 422 423 func rvSetInt16(rv reflect.Value, v int16) { 424 rv.SetInt(int64(v)) 425 } 426 427 func rvSetInt32(rv reflect.Value, v int32) { 428 rv.SetInt(int64(v)) 429 } 430 431 func rvSetInt64(rv reflect.Value, v int64) { 432 rv.SetInt(v) 433 } 434 435 func rvSetUint(rv reflect.Value, v uint) { 436 rv.SetUint(uint64(v)) 437 } 438 439 func rvSetUintptr(rv reflect.Value, v uintptr) { 440 rv.SetUint(uint64(v)) 441 } 442 443 func rvSetUint8(rv reflect.Value, v uint8) { 444 rv.SetUint(uint64(v)) 445 } 446 447 func rvSetUint16(rv reflect.Value, v uint16) { 448 rv.SetUint(uint64(v)) 449 } 450 451 func rvSetUint32(rv reflect.Value, v uint32) { 452 rv.SetUint(uint64(v)) 453 } 454 455 func rvSetUint64(rv reflect.Value, v uint64) { 456 rv.SetUint(v) 457 } 458 459 // ---------------- 460 461 func rvSetDirect(rv reflect.Value, v reflect.Value) { 462 rv.Set(v) 463 } 464 465 func rvSetDirectZero(rv reflect.Value) { 466 rv.Set(reflect.Zero(rv.Type())) 467 } 468 469 // func rvSet(rv reflect.Value, v reflect.Value) { 470 // rv.Set(v) 471 // } 472 473 func rvSetIntf(rv reflect.Value, v reflect.Value) { 474 rv.Set(v) 475 } 476 477 func rvSetZero(rv reflect.Value) { 478 rv.Set(reflect.Zero(rv.Type())) 479 } 480 481 func rvSlice(rv reflect.Value, length int) reflect.Value { 482 return rv.Slice(0, length) 483 } 484 485 func rvMakeSlice(rv reflect.Value, ti *typeInfo, xlen, xcap int) (v reflect.Value, set bool) { 486 v = reflect.MakeSlice(ti.rt, xlen, xcap) 487 if rv.Len() > 0 { 488 reflect.Copy(v, rv) 489 } 490 return 491 } 492 493 func rvGrowSlice(rv reflect.Value, ti *typeInfo, cap, incr int) (v reflect.Value, newcap int, set bool) { 494 newcap = int(growCap(uint(cap), uint(ti.elemsize), uint(incr))) 495 v = reflect.MakeSlice(ti.rt, newcap, newcap) 496 if rv.Len() > 0 { 497 reflect.Copy(v, rv) 498 } 499 return 500 } 501 502 // ---------------- 503 504 func rvSliceIndex(rv reflect.Value, i int, ti *typeInfo) reflect.Value { 505 return rv.Index(i) 506 } 507 508 func rvArrayIndex(rv reflect.Value, i int, ti *typeInfo) reflect.Value { 509 return rv.Index(i) 510 } 511 512 func rvSliceZeroCap(t reflect.Type) (v reflect.Value) { 513 return reflect.MakeSlice(t, 0, 0) 514 } 515 516 func rvLenSlice(rv reflect.Value) int { 517 return rv.Len() 518 } 519 520 func rvCapSlice(rv reflect.Value) int { 521 return rv.Cap() 522 } 523 524 func rvGetArrayBytes(rv reflect.Value, scratch []byte) (bs []byte) { 525 l := rv.Len() 526 if scratch == nil || rv.CanAddr() { 527 return rv.Slice(0, l).Bytes() 528 } 529 530 if l <= cap(scratch) { 531 bs = scratch[:l] 532 } else { 533 bs = make([]byte, l) 534 } 535 reflect.Copy(reflect.ValueOf(bs), rv) 536 return 537 } 538 539 func rvGetArray4Slice(rv reflect.Value) (v reflect.Value) { 540 v = rvZeroAddrK(reflectArrayOf(rvLenSlice(rv), rv.Type().Elem()), reflect.Array) 541 reflect.Copy(v, rv) 542 return 543 } 544 545 func rvGetSlice4Array(rv reflect.Value, v interface{}) { 546 // v is a pointer to a slice to be populated 547 548 // rv.Slice fails if address is not addressable, which can occur during encoding. 549 // Consequently, check if non-addressable, and if so, make new slice and copy into it first. 550 // MARKER: this *may* cause allocation if non-addressable, unfortunately. 551 552 rve := reflect.ValueOf(v).Elem() 553 l := rv.Len() 554 if rv.CanAddr() { 555 rve.Set(rv.Slice(0, l)) 556 } else { 557 rvs := reflect.MakeSlice(rve.Type(), l, l) 558 reflect.Copy(rvs, rv) 559 rve.Set(rvs) 560 } 561 // reflect.ValueOf(v).Elem().Set(rv.Slice(0, rv.Len())) 562 } 563 564 func rvCopySlice(dest, src reflect.Value, _ reflect.Type) { 565 reflect.Copy(dest, src) 566 } 567 568 // ------------ 569 570 func rvGetBool(rv reflect.Value) bool { 571 return rv.Bool() 572 } 573 574 func rvGetBytes(rv reflect.Value) []byte { 575 return rv.Bytes() 576 } 577 578 func rvGetTime(rv reflect.Value) time.Time { 579 return rv2i(rv).(time.Time) 580 } 581 582 func rvGetString(rv reflect.Value) string { 583 return rv.String() 584 } 585 586 func rvGetFloat64(rv reflect.Value) float64 { 587 return rv.Float() 588 } 589 590 func rvGetFloat32(rv reflect.Value) float32 { 591 return float32(rv.Float()) 592 } 593 594 func rvGetComplex64(rv reflect.Value) complex64 { 595 return complex64(rv.Complex()) 596 } 597 598 func rvGetComplex128(rv reflect.Value) complex128 { 599 return rv.Complex() 600 } 601 602 func rvGetInt(rv reflect.Value) int { 603 return int(rv.Int()) 604 } 605 606 func rvGetInt8(rv reflect.Value) int8 { 607 return int8(rv.Int()) 608 } 609 610 func rvGetInt16(rv reflect.Value) int16 { 611 return int16(rv.Int()) 612 } 613 614 func rvGetInt32(rv reflect.Value) int32 { 615 return int32(rv.Int()) 616 } 617 618 func rvGetInt64(rv reflect.Value) int64 { 619 return rv.Int() 620 } 621 622 func rvGetUint(rv reflect.Value) uint { 623 return uint(rv.Uint()) 624 } 625 626 func rvGetUint8(rv reflect.Value) uint8 { 627 return uint8(rv.Uint()) 628 } 629 630 func rvGetUint16(rv reflect.Value) uint16 { 631 return uint16(rv.Uint()) 632 } 633 634 func rvGetUint32(rv reflect.Value) uint32 { 635 return uint32(rv.Uint()) 636 } 637 638 func rvGetUint64(rv reflect.Value) uint64 { 639 return rv.Uint() 640 } 641 642 func rvGetUintptr(rv reflect.Value) uintptr { 643 return uintptr(rv.Uint()) 644 } 645 646 func rvLenMap(rv reflect.Value) int { 647 return rv.Len() 648 } 649 650 // func copybytes(to, from []byte) int { 651 // return copy(to, from) 652 // } 653 654 // func copybytestr(to []byte, from string) int { 655 // return copy(to, from) 656 // } 657 658 // func rvLenArray(rv reflect.Value) int { return rv.Len() } 659 660 // ------------ map range and map indexing ---------- 661 662 func mapStoresElemIndirect(elemsize uintptr) bool { return false } 663 664 func mapSet(m, k, v reflect.Value, keyFastKind mapKeyFastKind, _, _ bool) { 665 m.SetMapIndex(k, v) 666 } 667 668 func mapGet(m, k, v reflect.Value, keyFastKind mapKeyFastKind, _, _ bool) (vv reflect.Value) { 669 return m.MapIndex(k) 670 } 671 672 // func mapDelete(m, k reflect.Value) { 673 // m.SetMapIndex(k, reflect.Value{}) 674 // } 675 676 func mapAddrLoopvarRV(t reflect.Type, k reflect.Kind) (r reflect.Value) { 677 return // reflect.New(t).Elem() 678 } 679 680 // ---------- ENCODER optimized --------------- 681 682 func (e *Encoder) jsondriver() *jsonEncDriver { 683 return e.e.(*jsonEncDriver) 684 } 685 686 // ---------- DECODER optimized --------------- 687 688 func (d *Decoder) jsondriver() *jsonDecDriver { 689 return d.d.(*jsonDecDriver) 690 } 691 692 func (d *Decoder) stringZC(v []byte) (s string) { 693 return d.string(v) 694 } 695 696 func (d *Decoder) mapKeyString(callFnRvk *bool, kstrbs, kstr2bs *[]byte) string { 697 return d.string(*kstr2bs) 698 } 699 700 // ---------- structFieldInfo optimized --------------- 701 702 func (n *structFieldInfoPathNode) rvField(v reflect.Value) reflect.Value { 703 return v.Field(int(n.index)) 704 } 705 706 // ---------- others ---------------