helper_not_unsafe.go 15 KB

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