helper_not_unsafe.go 14 KB

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