helper.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // Copyright (c) 2012, 2013 Ugorji Nwoke. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license found in the LICENSE file.
  3. package codec
  4. // Contains code shared by both encode and decode.
  5. import (
  6. "encoding/binary"
  7. "fmt"
  8. "math"
  9. "reflect"
  10. "sort"
  11. "strings"
  12. "sync"
  13. "time"
  14. "unicode"
  15. "unicode/utf8"
  16. )
  17. const (
  18. structTagName = "codec"
  19. // Support
  20. // encoding.BinaryMarshaler: MarshalBinary() (data []byte, err error)
  21. // encoding.BinaryUnmarshaler: UnmarshalBinary(data []byte) error
  22. // This constant flag will enable or disable it.
  23. supportBinaryMarshal = true
  24. // Each Encoder or Decoder uses a cache of functions based on conditionals,
  25. // so that the conditionals are not run every time.
  26. //
  27. // Either a map or a slice is used to keep track of the functions.
  28. // The map is more natural, but has a higher cost than a slice/array.
  29. // This flag (useMapForCodecCache) controls which is used.
  30. useMapForCodecCache = false
  31. // for debugging, set this to false, to catch panic traces.
  32. // Note that this will always cause rpc tests to fail, since they need io.EOF sent via panic.
  33. recoverPanicToErr = true
  34. // Fast path functions try to create a fast path encode or decode implementation
  35. // for common maps and slices, by by-passing reflection altogether.
  36. fastpathEnabled = true
  37. )
  38. type charEncoding uint8
  39. const (
  40. c_RAW charEncoding = iota
  41. c_UTF8
  42. c_UTF16LE
  43. c_UTF16BE
  44. c_UTF32LE
  45. c_UTF32BE
  46. )
  47. // valueType is the stream type
  48. type valueType uint8
  49. const (
  50. valueTypeUnset valueType = iota
  51. valueTypeNil
  52. valueTypeInt
  53. valueTypeUint
  54. valueTypeFloat
  55. valueTypeBool
  56. valueTypeString
  57. valueTypeSymbol
  58. valueTypeBytes
  59. valueTypeMap
  60. valueTypeArray
  61. valueTypeTimestamp
  62. valueTypeExt
  63. valueTypeInvalid = 0xff
  64. )
  65. var (
  66. bigen = binary.BigEndian
  67. structInfoFieldName = "_struct"
  68. cachedTypeInfo = make(map[uintptr]*typeInfo, 4)
  69. cachedTypeInfoMutex sync.RWMutex
  70. intfSliceTyp = reflect.TypeOf([]interface{}(nil))
  71. intfTyp = intfSliceTyp.Elem()
  72. stringTyp = reflect.TypeOf("")
  73. timeTyp = reflect.TypeOf(time.Time{})
  74. rawExtTyp = reflect.TypeOf(RawExt{})
  75. uint8SliceTyp = reflect.TypeOf([]uint8(nil))
  76. mapBySliceTyp = reflect.TypeOf((*MapBySlice)(nil)).Elem()
  77. binaryMarshalerTyp = reflect.TypeOf((*binaryMarshaler)(nil)).Elem()
  78. binaryUnmarshalerTyp = reflect.TypeOf((*binaryUnmarshaler)(nil)).Elem()
  79. uint8SliceTypId = reflect.ValueOf(uint8SliceTyp).Pointer()
  80. rawExtTypId = reflect.ValueOf(rawExtTyp).Pointer()
  81. intfTypId = reflect.ValueOf(intfTyp).Pointer()
  82. timeTypId = reflect.ValueOf(timeTyp).Pointer()
  83. // mapBySliceTypId = reflect.ValueOf(mapBySliceTyp).Pointer()
  84. intBitsize uint8 = uint8(reflect.TypeOf(int(0)).Bits())
  85. uintBitsize uint8 = uint8(reflect.TypeOf(uint(0)).Bits())
  86. bsAll0x00 = []byte{0, 0, 0, 0, 0, 0, 0, 0}
  87. bsAll0xff = []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}
  88. )
  89. type binaryUnmarshaler interface {
  90. UnmarshalBinary(data []byte) error
  91. }
  92. type binaryMarshaler interface {
  93. MarshalBinary() (data []byte, err error)
  94. }
  95. // MapBySlice represents a slice which should be encoded as a map in the stream.
  96. // The slice contains a sequence of key-value pairs.
  97. type MapBySlice interface {
  98. MapBySlice()
  99. }
  100. // WARNING: DO NOT USE DIRECTLY. EXPORTED FOR GODOC BENEFIT. WILL BE REMOVED.
  101. //
  102. // BasicHandle encapsulates the common options and extension functions.
  103. type BasicHandle struct {
  104. extHandle
  105. EncodeOptions
  106. DecodeOptions
  107. }
  108. // Handle is the interface for a specific encoding format.
  109. //
  110. // Typically, a Handle is pre-configured before first time use,
  111. // and not modified while in use. Such a pre-configured Handle
  112. // is safe for concurrent access.
  113. type Handle interface {
  114. writeExt() bool
  115. getBasicHandle() *BasicHandle
  116. newEncDriver(w encWriter) encDriver
  117. newDecDriver(r decReader) decDriver
  118. }
  119. // RawExt represents raw unprocessed extension data.
  120. type RawExt struct {
  121. Tag byte
  122. Data []byte
  123. }
  124. type extTypeTagFn struct {
  125. rtid uintptr
  126. rt reflect.Type
  127. tag byte
  128. encFn func(reflect.Value) ([]byte, error)
  129. decFn func(reflect.Value, []byte) error
  130. }
  131. type extHandle []*extTypeTagFn
  132. // AddExt registers an encode and decode function for a reflect.Type.
  133. // Note that the type must be a named type, and specifically not
  134. // a pointer or Interface. An error is returned if that is not honored.
  135. //
  136. // To Deregister an ext, call AddExt with 0 tag, nil encfn and nil decfn.
  137. func (o *extHandle) AddExt(
  138. rt reflect.Type,
  139. tag byte,
  140. encfn func(reflect.Value) ([]byte, error),
  141. decfn func(reflect.Value, []byte) error,
  142. ) (err error) {
  143. // o is a pointer, because we may need to initialize it
  144. if rt.PkgPath() == "" || rt.Kind() == reflect.Interface {
  145. err = fmt.Errorf("codec.Handle.AddExt: Takes named type, especially not a pointer or interface: %T",
  146. reflect.Zero(rt).Interface())
  147. return
  148. }
  149. // o cannot be nil, since it is always embedded in a Handle.
  150. // if nil, let it panic.
  151. // if o == nil {
  152. // err = errors.New("codec.Handle.AddExt: extHandle cannot be a nil pointer.")
  153. // return
  154. // }
  155. rtid := reflect.ValueOf(rt).Pointer()
  156. for _, v := range *o {
  157. if v.rtid == rtid {
  158. v.tag, v.encFn, v.decFn = tag, encfn, decfn
  159. return
  160. }
  161. }
  162. *o = append(*o, &extTypeTagFn{rtid, rt, tag, encfn, decfn})
  163. return
  164. }
  165. func (o extHandle) getExt(rtid uintptr) *extTypeTagFn {
  166. for _, v := range o {
  167. if v.rtid == rtid {
  168. return v
  169. }
  170. }
  171. return nil
  172. }
  173. func (o extHandle) getExtForTag(tag byte) *extTypeTagFn {
  174. for _, v := range o {
  175. if v.tag == tag {
  176. return v
  177. }
  178. }
  179. return nil
  180. }
  181. func (o extHandle) getDecodeExtForTag(tag byte) (
  182. rv reflect.Value, fn func(reflect.Value, []byte) error) {
  183. if x := o.getExtForTag(tag); x != nil {
  184. // ext is only registered for base
  185. rv = reflect.New(x.rt).Elem()
  186. fn = x.decFn
  187. }
  188. return
  189. }
  190. func (o extHandle) getDecodeExt(rtid uintptr) (tag byte, fn func(reflect.Value, []byte) error) {
  191. if x := o.getExt(rtid); x != nil {
  192. tag = x.tag
  193. fn = x.decFn
  194. }
  195. return
  196. }
  197. func (o extHandle) getEncodeExt(rtid uintptr) (tag byte, fn func(reflect.Value) ([]byte, error)) {
  198. if x := o.getExt(rtid); x != nil {
  199. tag = x.tag
  200. fn = x.encFn
  201. }
  202. return
  203. }
  204. type structFieldInfo struct {
  205. encName string // encode name
  206. // only one of 'i' or 'is' can be set. If 'i' is -1, then 'is' has been set.
  207. is []int // (recursive/embedded) field index in struct
  208. i int16 // field index in struct
  209. omitEmpty bool
  210. toArray bool // if field is _struct, is the toArray set?
  211. // tag string // tag
  212. // name string // field name
  213. // encNameBs []byte // encoded name as byte stream
  214. // ikind int // kind of the field as an int i.e. int(reflect.Kind)
  215. }
  216. func parseStructFieldInfo(fname string, stag string) *structFieldInfo {
  217. if fname == "" {
  218. panic("parseStructFieldInfo: No Field Name")
  219. }
  220. si := structFieldInfo{
  221. // name: fname,
  222. encName: fname,
  223. // tag: stag,
  224. }
  225. if stag != "" {
  226. for i, s := range strings.Split(stag, ",") {
  227. if i == 0 {
  228. if s != "" {
  229. si.encName = s
  230. }
  231. } else {
  232. switch s {
  233. case "omitempty":
  234. si.omitEmpty = true
  235. case "toarray":
  236. si.toArray = true
  237. }
  238. }
  239. }
  240. }
  241. // si.encNameBs = []byte(si.encName)
  242. return &si
  243. }
  244. type sfiSortedByEncName []*structFieldInfo
  245. func (p sfiSortedByEncName) Len() int {
  246. return len(p)
  247. }
  248. func (p sfiSortedByEncName) Less(i, j int) bool {
  249. return p[i].encName < p[j].encName
  250. }
  251. func (p sfiSortedByEncName) Swap(i, j int) {
  252. p[i], p[j] = p[j], p[i]
  253. }
  254. // typeInfo keeps information about each type referenced in the encode/decode sequence.
  255. //
  256. // During an encode/decode sequence, we work as below:
  257. // - If base is a built in type, en/decode base value
  258. // - If base is registered as an extension, en/decode base value
  259. // - If type is binary(M/Unm)arshaler, call Binary(M/Unm)arshal method
  260. // - Else decode appropriately based on the reflect.Kind
  261. type typeInfo struct {
  262. sfi []*structFieldInfo // sorted. Used when enc/dec struct to map.
  263. sfip []*structFieldInfo // unsorted. Used when enc/dec struct to array.
  264. rt reflect.Type
  265. rtid uintptr
  266. // baseId gives pointer to the base reflect.Type, after deferencing
  267. // the pointers. E.g. base type of ***time.Time is time.Time.
  268. base reflect.Type
  269. baseId uintptr
  270. baseIndir int8 // number of indirections to get to base
  271. mbs bool // base type (T or *T) is a MapBySlice
  272. m bool // base type (T or *T) is a binaryMarshaler
  273. unm bool // base type (T or *T) is a binaryUnmarshaler
  274. mIndir int8 // number of indirections to get to binaryMarshaler type
  275. unmIndir int8 // number of indirections to get to binaryUnmarshaler type
  276. toArray bool // whether this (struct) type should be encoded as an array
  277. }
  278. func (ti *typeInfo) indexForEncName(name string) int {
  279. //tisfi := ti.sfi
  280. const binarySearchThreshold = 16
  281. if sfilen := len(ti.sfi); sfilen < binarySearchThreshold {
  282. // linear search. faster than binary search in my testing up to 16-field structs.
  283. for i, si := range ti.sfi {
  284. if si.encName == name {
  285. return i
  286. }
  287. }
  288. } else {
  289. // binary search. adapted from sort/search.go.
  290. h, i, j := 0, 0, sfilen
  291. for i < j {
  292. h = i + (j-i)/2
  293. if ti.sfi[h].encName < name {
  294. i = h + 1
  295. } else {
  296. j = h
  297. }
  298. }
  299. if i < sfilen && ti.sfi[i].encName == name {
  300. return i
  301. }
  302. }
  303. return -1
  304. }
  305. func getTypeInfo(rtid uintptr, rt reflect.Type) (pti *typeInfo) {
  306. var ok bool
  307. cachedTypeInfoMutex.RLock()
  308. pti, ok = cachedTypeInfo[rtid]
  309. cachedTypeInfoMutex.RUnlock()
  310. if ok {
  311. return
  312. }
  313. cachedTypeInfoMutex.Lock()
  314. defer cachedTypeInfoMutex.Unlock()
  315. if pti, ok = cachedTypeInfo[rtid]; ok {
  316. return
  317. }
  318. ti := typeInfo{rt: rt, rtid: rtid}
  319. pti = &ti
  320. var indir int8
  321. if ok, indir = implementsIntf(rt, binaryMarshalerTyp); ok {
  322. ti.m, ti.mIndir = true, indir
  323. }
  324. if ok, indir = implementsIntf(rt, binaryUnmarshalerTyp); ok {
  325. ti.unm, ti.unmIndir = true, indir
  326. }
  327. if ok, _ = implementsIntf(rt, mapBySliceTyp); ok {
  328. ti.mbs = true
  329. }
  330. pt := rt
  331. var ptIndir int8
  332. // for ; pt.Kind() == reflect.Ptr; pt, ptIndir = pt.Elem(), ptIndir+1 { }
  333. for pt.Kind() == reflect.Ptr {
  334. pt = pt.Elem()
  335. ptIndir++
  336. }
  337. if ptIndir == 0 {
  338. ti.base = rt
  339. ti.baseId = rtid
  340. } else {
  341. ti.base = pt
  342. ti.baseId = reflect.ValueOf(pt).Pointer()
  343. ti.baseIndir = ptIndir
  344. }
  345. if rt.Kind() == reflect.Struct {
  346. var siInfo *structFieldInfo
  347. if f, ok := rt.FieldByName(structInfoFieldName); ok {
  348. siInfo = parseStructFieldInfo(structInfoFieldName, f.Tag.Get(structTagName))
  349. ti.toArray = siInfo.toArray
  350. }
  351. sfip := make([]*structFieldInfo, 0, rt.NumField())
  352. rgetTypeInfo(rt, nil, make(map[string]bool), &sfip, siInfo)
  353. // // try to put all si close together
  354. // const tryToPutAllStructFieldInfoTogether = true
  355. // if tryToPutAllStructFieldInfoTogether {
  356. // sfip2 := make([]structFieldInfo, len(sfip))
  357. // for i, si := range sfip {
  358. // sfip2[i] = *si
  359. // }
  360. // for i := range sfip {
  361. // sfip[i] = &sfip2[i]
  362. // }
  363. // }
  364. ti.sfip = make([]*structFieldInfo, len(sfip))
  365. ti.sfi = make([]*structFieldInfo, len(sfip))
  366. copy(ti.sfip, sfip)
  367. sort.Sort(sfiSortedByEncName(sfip))
  368. copy(ti.sfi, sfip)
  369. }
  370. // sfi = sfip
  371. cachedTypeInfo[rtid] = pti
  372. return
  373. }
  374. func rgetTypeInfo(rt reflect.Type, indexstack []int, fnameToHastag map[string]bool,
  375. sfi *[]*structFieldInfo, siInfo *structFieldInfo,
  376. ) {
  377. // for rt.Kind() == reflect.Ptr {
  378. // // indexstack = append(indexstack, 0)
  379. // rt = rt.Elem()
  380. // }
  381. for j := 0; j < rt.NumField(); j++ {
  382. f := rt.Field(j)
  383. stag := f.Tag.Get(structTagName)
  384. if stag == "-" {
  385. continue
  386. }
  387. if r1, _ := utf8.DecodeRuneInString(f.Name); r1 == utf8.RuneError || !unicode.IsUpper(r1) {
  388. continue
  389. }
  390. // if anonymous and there is no struct tag and its a struct (or pointer to struct), inline it.
  391. if f.Anonymous && stag == "" {
  392. ft := f.Type
  393. for ft.Kind() == reflect.Ptr {
  394. ft = ft.Elem()
  395. }
  396. if ft.Kind() == reflect.Struct {
  397. indexstack2 := append(append(make([]int, 0, len(indexstack)+4), indexstack...), j)
  398. rgetTypeInfo(ft, indexstack2, fnameToHastag, sfi, siInfo)
  399. continue
  400. }
  401. }
  402. // do not let fields with same name in embedded structs override field at higher level.
  403. // this must be done after anonymous check, to allow anonymous field
  404. // still include their child fields
  405. if _, ok := fnameToHastag[f.Name]; ok {
  406. continue
  407. }
  408. si := parseStructFieldInfo(f.Name, stag)
  409. // si.ikind = int(f.Type.Kind())
  410. if len(indexstack) == 0 {
  411. si.i = int16(j)
  412. } else {
  413. si.i = -1
  414. si.is = append(append(make([]int, 0, len(indexstack)+4), indexstack...), j)
  415. }
  416. if siInfo != nil {
  417. if siInfo.omitEmpty {
  418. si.omitEmpty = true
  419. }
  420. }
  421. *sfi = append(*sfi, si)
  422. fnameToHastag[f.Name] = stag != ""
  423. }
  424. }
  425. func panicToErr(err *error) {
  426. if recoverPanicToErr {
  427. if x := recover(); x != nil {
  428. //debug.PrintStack()
  429. panicValToErr(x, err)
  430. }
  431. }
  432. }
  433. func doPanic(tag string, format string, params ...interface{}) {
  434. params2 := make([]interface{}, len(params)+1)
  435. params2[0] = tag
  436. copy(params2[1:], params)
  437. panic(fmt.Errorf("%s: "+format, params2...))
  438. }
  439. func checkOverflowFloat32(f float64, doCheck bool) {
  440. if !doCheck {
  441. return
  442. }
  443. // check overflow (logic adapted from std pkg reflect/value.go OverflowFloat()
  444. f2 := f
  445. if f2 < 0 {
  446. f2 = -f
  447. }
  448. if math.MaxFloat32 < f2 && f2 <= math.MaxFloat64 {
  449. decErr("Overflow float32 value: %v", f2)
  450. }
  451. }
  452. func checkOverflow(ui uint64, i int64, bitsize uint8) {
  453. // check overflow (logic adapted from std pkg reflect/value.go OverflowUint()
  454. if bitsize == 0 {
  455. return
  456. }
  457. if i != 0 {
  458. if trunc := (i << (64 - bitsize)) >> (64 - bitsize); i != trunc {
  459. decErr("Overflow int value: %v", i)
  460. }
  461. }
  462. if ui != 0 {
  463. if trunc := (ui << (64 - bitsize)) >> (64 - bitsize); ui != trunc {
  464. decErr("Overflow uint value: %v", ui)
  465. }
  466. }
  467. }