helper.go 16 KB

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