Debian Codegraph @

codec/helper_not_unsafe.go

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