decode.go 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. // Copyright (c) 2012-2015 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. import (
  5. "encoding"
  6. "io"
  7. "reflect"
  8. // "runtime/debug"
  9. )
  10. // Some tagging information for error messages.
  11. const (
  12. msgTagDec = "codec.decoder"
  13. msgBadDesc = "Unrecognized descriptor byte"
  14. msgDecCannotExpandArr = "cannot expand go array from %v to stream length: %v"
  15. )
  16. // fastpathsDec holds the rtid (reflect.Type Pointer) to fast decode function for a selected slice/map type.
  17. var fastpathsDec = make(map[uintptr]func(*decFnInfo, reflect.Value))
  18. // decReader abstracts the reading source, allowing implementations that can
  19. // read from an io.Reader or directly off a byte slice with zero-copying.
  20. type decReader interface {
  21. unreadn1()
  22. readn(n int) []byte
  23. readb([]byte)
  24. readn1() uint8
  25. readUint16() uint16
  26. readUint32() uint32
  27. readUint64() uint64
  28. }
  29. type decDriver interface {
  30. initReadNext()
  31. // this will call initReadNext implicitly, and then check if the next token is a break.
  32. checkBreak() bool
  33. tryDecodeAsNil() bool
  34. // check if a container type: vt is one of: Bytes, String, Nil, Slice or Map
  35. isContainerType(vt valueType) bool
  36. isBuiltinType(rt uintptr) bool
  37. decodeBuiltin(rt uintptr, v interface{})
  38. //decodeNaked: Numbers are decoded as int64, uint64, float64 only (no smaller sized number types).
  39. //for extensions, decodeNaked must completely decode them as a *RawExt.
  40. decodeNaked(*Decoder) (v interface{}, vt valueType, decodeFurther bool)
  41. decodeInt(bitsize uint8) (i int64)
  42. decodeUint(bitsize uint8) (ui uint64)
  43. decodeFloat(chkOverflow32 bool) (f float64)
  44. decodeBool() (b bool)
  45. // decodeString can also decode symbols
  46. decodeString() (s string)
  47. decodeBytes(bs []byte) (bsOut []byte, changed bool)
  48. // decodeExt will decode into a *RawExt or into an extension.
  49. decodeExt(rv reflect.Value, xtag uint64, ext Ext, d *Decoder) (realxtag uint64)
  50. // decodeExt(verifyTag bool, tag byte) (xtag byte, xbs []byte)
  51. readMapStart() int
  52. readArrayStart() int
  53. readMapEnd()
  54. readArrayEnd()
  55. readArrayEntrySeparator()
  56. readMapEntrySeparator()
  57. readMapKVSeparator()
  58. }
  59. // decDrivers may implement this interface to bypass allocation
  60. type decDriverStringAsBytes interface {
  61. decStringAsBytes(bs []byte) []byte
  62. }
  63. type decNoMapArrayEnd struct{}
  64. func (_ decNoMapArrayEnd) readMapEnd() {}
  65. func (_ decNoMapArrayEnd) readArrayEnd() {}
  66. type decNoMapArraySeparator struct{}
  67. func (_ decNoMapArraySeparator) readArrayEntrySeparator() {}
  68. func (_ decNoMapArraySeparator) readMapEntrySeparator() {}
  69. func (_ decNoMapArraySeparator) readMapKVSeparator() {}
  70. type DecodeOptions struct {
  71. // An instance of MapType is used during schema-less decoding of a map in the stream.
  72. // If nil, we use map[interface{}]interface{}
  73. MapType reflect.Type
  74. // An instance of SliceType is used during schema-less decoding of an array in the stream.
  75. // If nil, we use []interface{}
  76. SliceType reflect.Type
  77. // ErrorIfNoField controls whether an error is returned when decoding a map
  78. // from a codec stream into a struct, and no matching struct field is found.
  79. ErrorIfNoField bool
  80. // If true, use the int64 during schema-less decoding of unsigned values (not uint64).
  81. SignedInteger bool
  82. }
  83. // ------------------------------------
  84. // ioDecReader is a decReader that reads off an io.Reader
  85. type ioDecReader struct {
  86. r io.Reader
  87. br io.ByteScanner
  88. x [8]byte //temp byte array re-used internally for efficiency
  89. l byte // last byte
  90. ls uint8 // last byte status: 0: unset, 1: read, 2: unread
  91. }
  92. func (z *ioDecReader) readn(n int) (bs []byte) {
  93. if n <= 0 {
  94. return
  95. }
  96. bs = make([]byte, n)
  97. if _, err := io.ReadAtLeast(z.r, bs, n); err != nil {
  98. panic(err)
  99. }
  100. return
  101. }
  102. func (z *ioDecReader) readb(bs []byte) {
  103. if _, err := io.ReadAtLeast(z.r, bs, len(bs)); err != nil {
  104. panic(err)
  105. }
  106. }
  107. func (z *ioDecReader) readn1() uint8 {
  108. if z.br != nil {
  109. if b, err := z.br.ReadByte(); err == nil {
  110. return b
  111. } else {
  112. panic(err)
  113. }
  114. }
  115. if z.ls == 2 {
  116. z.ls = 0
  117. } else {
  118. z.readb(z.x[:1])
  119. z.l = z.x[0]
  120. z.ls = 1
  121. }
  122. return z.l
  123. }
  124. func (z *ioDecReader) unreadn1() {
  125. if z.br != nil {
  126. if err := z.br.UnreadByte(); err != nil {
  127. panic(err)
  128. }
  129. return
  130. }
  131. if z.ls == 2 {
  132. decErr("cannot unread when last byte has been unread")
  133. }
  134. if z.ls == 1 {
  135. z.ls = 2
  136. return
  137. }
  138. decErr("cannot unread when no byte has been read")
  139. }
  140. func (z *ioDecReader) readUint16() uint16 {
  141. z.readb(z.x[:2])
  142. return bigen.Uint16(z.x[:2])
  143. }
  144. func (z *ioDecReader) readUint32() uint32 {
  145. z.readb(z.x[:4])
  146. return bigen.Uint32(z.x[:4])
  147. }
  148. func (z *ioDecReader) readUint64() uint64 {
  149. z.readb(z.x[:8])
  150. return bigen.Uint64(z.x[:8])
  151. }
  152. // ------------------------------------
  153. // bytesDecReader is a decReader that reads off a byte slice with zero copying
  154. type bytesDecReader struct {
  155. b []byte // data
  156. c int // cursor
  157. a int // available
  158. }
  159. func (z *bytesDecReader) consume(n int) (oldcursor int) {
  160. if z.a == 0 {
  161. panic(io.EOF)
  162. }
  163. if n > z.a {
  164. decErr("cannot read %v bytes, when only %v available", n, z.a)
  165. }
  166. // z.checkAvailable(n)
  167. oldcursor = z.c
  168. z.c = oldcursor + n
  169. z.a = z.a - n
  170. return
  171. }
  172. func (z *bytesDecReader) unreadn1() {
  173. if z.c == 0 || len(z.b) == 0 {
  174. decErr("cannot unread last byte read")
  175. }
  176. z.c--
  177. z.a++
  178. return
  179. }
  180. func (z *bytesDecReader) readn(n int) (bs []byte) {
  181. if n <= 0 {
  182. return
  183. }
  184. c0 := z.consume(n)
  185. bs = z.b[c0:z.c]
  186. return
  187. }
  188. func (z *bytesDecReader) readb(bs []byte) {
  189. copy(bs, z.readn(len(bs)))
  190. }
  191. func (z *bytesDecReader) readn1() uint8 {
  192. c0 := z.consume(1)
  193. return z.b[c0]
  194. }
  195. // Use binaryEncoding helper for 4 and 8 bits, but inline it for 2 bits
  196. // creating temp slice variable and copying it to helper function is expensive
  197. // for just 2 bits.
  198. func (z *bytesDecReader) readUint16() uint16 {
  199. c0 := z.consume(2)
  200. return uint16(z.b[c0+1]) | uint16(z.b[c0])<<8
  201. }
  202. func (z *bytesDecReader) readUint32() uint32 {
  203. c0 := z.consume(4)
  204. return bigen.Uint32(z.b[c0:z.c])
  205. }
  206. func (z *bytesDecReader) readUint64() uint64 {
  207. c0 := z.consume(8)
  208. return bigen.Uint64(z.b[c0:z.c])
  209. }
  210. // ------------------------------------
  211. // decFnInfo has methods for registering handling decoding of a specific type
  212. // based on some characteristics (builtin, extension, reflect Kind, etc)
  213. type decFnInfo struct {
  214. ti *typeInfo
  215. d *Decoder
  216. dd decDriver
  217. xfFn Ext
  218. xfTag uint64
  219. array bool
  220. }
  221. // ----------------------------------------
  222. type decFn struct {
  223. i *decFnInfo
  224. f func(*decFnInfo, reflect.Value)
  225. }
  226. func (f *decFnInfo) builtin(rv reflect.Value) {
  227. f.dd.decodeBuiltin(f.ti.rtid, rv.Addr().Interface())
  228. }
  229. func (f *decFnInfo) rawExt(rv reflect.Value) {
  230. f.dd.decodeExt(rv, 0, nil, f.d)
  231. }
  232. func (f *decFnInfo) ext(rv reflect.Value) {
  233. f.dd.decodeExt(rv, f.xfTag, f.xfFn, f.d)
  234. }
  235. func (f *decFnInfo) binaryUnmarshal(rv reflect.Value) {
  236. var bm encoding.BinaryUnmarshaler
  237. if f.ti.bunmIndir == -1 {
  238. bm = rv.Addr().Interface().(encoding.BinaryUnmarshaler)
  239. } else if f.ti.bunmIndir == 0 {
  240. bm = rv.Interface().(encoding.BinaryUnmarshaler)
  241. } else {
  242. for j, k := int8(0), f.ti.bunmIndir; j < k; j++ {
  243. if rv.IsNil() {
  244. rv.Set(reflect.New(rv.Type().Elem()))
  245. }
  246. rv = rv.Elem()
  247. }
  248. bm = rv.Interface().(encoding.BinaryUnmarshaler)
  249. }
  250. xbs, _ := f.dd.decodeBytes(nil)
  251. if fnerr := bm.UnmarshalBinary(xbs); fnerr != nil {
  252. panic(fnerr)
  253. }
  254. }
  255. func (f *decFnInfo) textUnmarshal(rv reflect.Value) {
  256. var tm encoding.TextUnmarshaler
  257. if f.ti.tunmIndir == -1 {
  258. tm = rv.Addr().Interface().(encoding.TextUnmarshaler)
  259. } else if f.ti.tunmIndir == 0 {
  260. tm = rv.Interface().(encoding.TextUnmarshaler)
  261. } else {
  262. for j, k := int8(0), f.ti.tunmIndir; j < k; j++ {
  263. if rv.IsNil() {
  264. rv.Set(reflect.New(rv.Type().Elem()))
  265. }
  266. rv = rv.Elem()
  267. }
  268. tm = rv.Interface().(encoding.TextUnmarshaler)
  269. }
  270. var fnerr error
  271. if sb, sbok := f.dd.(decDriverStringAsBytes); sbok {
  272. fnerr = tm.UnmarshalText(sb.decStringAsBytes(f.d.b[:0]))
  273. } else {
  274. fnerr = tm.UnmarshalText([]byte(f.dd.decodeString()))
  275. }
  276. if fnerr != nil {
  277. panic(fnerr)
  278. }
  279. }
  280. func (f *decFnInfo) kErr(rv reflect.Value) {
  281. decErr("no decoding function defined for kind %v", rv.Kind())
  282. }
  283. func (f *decFnInfo) kString(rv reflect.Value) {
  284. rv.SetString(f.dd.decodeString())
  285. }
  286. func (f *decFnInfo) kBool(rv reflect.Value) {
  287. rv.SetBool(f.dd.decodeBool())
  288. }
  289. func (f *decFnInfo) kInt(rv reflect.Value) {
  290. rv.SetInt(f.dd.decodeInt(intBitsize))
  291. }
  292. func (f *decFnInfo) kInt64(rv reflect.Value) {
  293. rv.SetInt(f.dd.decodeInt(64))
  294. }
  295. func (f *decFnInfo) kInt32(rv reflect.Value) {
  296. rv.SetInt(f.dd.decodeInt(32))
  297. }
  298. func (f *decFnInfo) kInt8(rv reflect.Value) {
  299. rv.SetInt(f.dd.decodeInt(8))
  300. }
  301. func (f *decFnInfo) kInt16(rv reflect.Value) {
  302. rv.SetInt(f.dd.decodeInt(16))
  303. }
  304. func (f *decFnInfo) kFloat32(rv reflect.Value) {
  305. rv.SetFloat(f.dd.decodeFloat(true))
  306. }
  307. func (f *decFnInfo) kFloat64(rv reflect.Value) {
  308. rv.SetFloat(f.dd.decodeFloat(false))
  309. }
  310. func (f *decFnInfo) kUint8(rv reflect.Value) {
  311. rv.SetUint(f.dd.decodeUint(8))
  312. }
  313. func (f *decFnInfo) kUint64(rv reflect.Value) {
  314. rv.SetUint(f.dd.decodeUint(64))
  315. }
  316. func (f *decFnInfo) kUint(rv reflect.Value) {
  317. rv.SetUint(f.dd.decodeUint(uintBitsize))
  318. }
  319. func (f *decFnInfo) kUint32(rv reflect.Value) {
  320. rv.SetUint(f.dd.decodeUint(32))
  321. }
  322. func (f *decFnInfo) kUint16(rv reflect.Value) {
  323. rv.SetUint(f.dd.decodeUint(16))
  324. }
  325. // func (f *decFnInfo) kPtr(rv reflect.Value) {
  326. // debugf(">>>>>>> ??? decode kPtr called - shouldn't get called")
  327. // if rv.IsNil() {
  328. // rv.Set(reflect.New(rv.Type().Elem()))
  329. // }
  330. // f.d.decodeValue(rv.Elem())
  331. // }
  332. func (f *decFnInfo) kInterface(rv reflect.Value) {
  333. // debugf("\t===> kInterface")
  334. if !rv.IsNil() {
  335. f.d.decodeValue(rv.Elem(), decFn{})
  336. return
  337. }
  338. // nil interface:
  339. // use some hieristics to set the nil interface to an
  340. // appropriate value based on the first byte read (byte descriptor bd)
  341. v, vt, decodeFurther := f.dd.decodeNaked(f.d)
  342. if vt == valueTypeNil {
  343. return
  344. }
  345. // Cannot decode into nil interface with methods (e.g. error, io.Reader, etc)
  346. // if non-nil value in stream.
  347. if num := f.ti.rt.NumMethod(); num > 0 {
  348. decErr("cannot decode non-nil codec value into nil %v (%v methods)",
  349. f.ti.rt, num)
  350. }
  351. var rvn reflect.Value
  352. var useRvn bool
  353. switch vt {
  354. case valueTypeMap:
  355. if f.d.h.MapType == nil {
  356. var m2 map[interface{}]interface{}
  357. v = &m2
  358. } else {
  359. rvn = reflect.New(f.d.h.MapType).Elem()
  360. useRvn = true
  361. }
  362. case valueTypeArray:
  363. if f.d.h.SliceType == nil {
  364. var m2 []interface{}
  365. v = &m2
  366. } else {
  367. rvn = reflect.New(f.d.h.SliceType).Elem()
  368. useRvn = true
  369. }
  370. case valueTypeExt:
  371. re := v.(*RawExt)
  372. bfn := f.d.h.getExtForTag(re.Tag)
  373. if bfn == nil {
  374. rvn = reflect.ValueOf(*re)
  375. } else {
  376. rvn = reflect.New(bfn.rt).Elem()
  377. if re.Data != nil {
  378. bfn.ext.ReadExt(rvn, re.Data)
  379. } else {
  380. bfn.ext.UpdateExt(rvn, re.Value)
  381. }
  382. }
  383. rv.Set(rvn)
  384. return
  385. }
  386. if decodeFurther {
  387. if useRvn {
  388. f.d.decodeValue(rvn, decFn{})
  389. } else if v != nil {
  390. // this v is a pointer, so we need to dereference it when done
  391. f.d.decode(v)
  392. rvn = reflect.ValueOf(v).Elem()
  393. useRvn = true
  394. }
  395. }
  396. if useRvn {
  397. rv.Set(rvn)
  398. } else if v != nil {
  399. rv.Set(reflect.ValueOf(v))
  400. }
  401. }
  402. func (f *decFnInfo) kStruct(rv reflect.Value) {
  403. fti := f.ti
  404. if f.dd.isContainerType(valueTypeMap) {
  405. containerLen := f.dd.readMapStart()
  406. if containerLen == 0 {
  407. f.dd.readMapEnd()
  408. return
  409. }
  410. tisfi := fti.sfi
  411. for j := 0; ; j++ {
  412. if containerLen >= 0 {
  413. if j >= containerLen {
  414. break
  415. }
  416. } else if f.dd.checkBreak() {
  417. break
  418. }
  419. if j > 0 {
  420. f.dd.readMapEntrySeparator()
  421. }
  422. // var rvkencname string
  423. // ddecode(&rvkencname)
  424. f.dd.initReadNext()
  425. rvkencname := f.dd.decodeString()
  426. f.dd.readMapKVSeparator()
  427. // rvksi := ti.getForEncName(rvkencname)
  428. if k := fti.indexForEncName(rvkencname); k > -1 {
  429. sfik := tisfi[k]
  430. if sfik.i != -1 {
  431. f.d.decodeValue(rv.Field(int(sfik.i)), decFn{})
  432. } else {
  433. f.d.decEmbeddedField(rv, sfik.is)
  434. }
  435. // f.d.decodeValue(ti.field(k, rv))
  436. } else {
  437. if f.d.h.ErrorIfNoField {
  438. decErr("no matching struct field found when decoding stream map with key %v",
  439. rvkencname)
  440. } else {
  441. var nilintf0 interface{}
  442. f.d.decodeValue(reflect.ValueOf(&nilintf0).Elem(), decFn{})
  443. }
  444. }
  445. }
  446. f.dd.readMapEnd()
  447. } else if f.dd.isContainerType(valueTypeArray) {
  448. containerLen := f.dd.readArrayStart()
  449. if containerLen == 0 {
  450. f.dd.readArrayEnd()
  451. return
  452. }
  453. for j, si := range fti.sfip {
  454. if containerLen >= 0 {
  455. if j == containerLen {
  456. break
  457. }
  458. } else if f.dd.checkBreak() {
  459. break
  460. }
  461. if j > 0 {
  462. f.dd.readArrayEntrySeparator()
  463. }
  464. if si.i != -1 {
  465. f.d.decodeValue(rv.Field(int(si.i)), decFn{})
  466. } else {
  467. f.d.decEmbeddedField(rv, si.is)
  468. }
  469. }
  470. if containerLen > len(fti.sfip) {
  471. // read remaining values and throw away
  472. for j := len(fti.sfip); j < containerLen; j++ {
  473. var nilintf0 interface{}
  474. if j > 0 {
  475. f.dd.readArrayEntrySeparator()
  476. }
  477. f.d.decodeValue(reflect.ValueOf(&nilintf0).Elem(), decFn{})
  478. }
  479. }
  480. f.dd.readArrayEnd()
  481. } else {
  482. decErr("only encoded map or array can be decoded into a struct")
  483. }
  484. }
  485. func (f *decFnInfo) kSlice(rv reflect.Value) {
  486. // A slice can be set from a map or array in stream. This way, the order can be kept (as order is lost with map).
  487. if f.dd.isContainerType(valueTypeBytes) || f.dd.isContainerType(valueTypeString) {
  488. if f.ti.rtid == uint8SliceTypId || f.ti.rt.Elem().Kind() == reflect.Uint8 {
  489. rvbs := rv.Bytes()
  490. if bs2, changed2 := f.dd.decodeBytes(rvbs); changed2 {
  491. if rv.CanSet() {
  492. rv.SetBytes(bs2)
  493. } else {
  494. copy(rvbs, bs2)
  495. }
  496. }
  497. return
  498. }
  499. }
  500. slh := decSliceHelper{dd: f.dd}
  501. containerLenS := slh.start()
  502. // an array can never return a nil slice. so no need to check f.array here.
  503. if rv.IsNil() {
  504. if containerLenS < 0 {
  505. rv.Set(reflect.MakeSlice(f.ti.rt, 0, 0))
  506. } else {
  507. rv.Set(reflect.MakeSlice(f.ti.rt, containerLenS, containerLenS))
  508. }
  509. }
  510. if containerLenS == 0 {
  511. rv.SetLen(0)
  512. f.dd.readArrayEnd()
  513. return
  514. }
  515. rv0 := rv
  516. rvChanged := false
  517. if rvcap, rvlen := rv.Len(), rv.Cap(); containerLenS > rvcap {
  518. if f.array { // !rv.CanSet()
  519. decErr(msgDecCannotExpandArr, rvcap, containerLenS)
  520. }
  521. rv = reflect.MakeSlice(f.ti.rt, containerLenS, containerLenS)
  522. if rvlen > 0 {
  523. reflect.Copy(rv, rv0)
  524. }
  525. rvChanged = true
  526. } else if containerLenS > rvlen {
  527. rv.SetLen(containerLenS)
  528. }
  529. rtelem0 := f.ti.rt.Elem()
  530. rtelem := rtelem0
  531. for rtelem.Kind() == reflect.Ptr {
  532. rtelem = rtelem.Elem()
  533. }
  534. fn := f.d.getDecFn(rtelem)
  535. // for j := 0; j < containerLenS; j++ {
  536. for j := 0; ; j++ {
  537. if containerLenS >= 0 {
  538. if j >= containerLenS {
  539. break
  540. }
  541. } else if f.dd.checkBreak() {
  542. break
  543. }
  544. // if indefinite, etc, then expand the slice if necessary
  545. if j >= rv.Len() {
  546. // TODO: Need to update this appropriately
  547. rv = reflect.Append(rv, reflect.Zero(rtelem0))
  548. rvChanged = true
  549. }
  550. if j > 0 {
  551. slh.sep(j)
  552. }
  553. f.d.decodeValue(rv.Index(j), fn)
  554. }
  555. slh.end()
  556. if rvChanged {
  557. rv0.Set(rv)
  558. }
  559. }
  560. func (f *decFnInfo) kArray(rv reflect.Value) {
  561. // f.d.decodeValue(rv.Slice(0, rv.Len()))
  562. f.kSlice(rv.Slice(0, rv.Len()))
  563. }
  564. func (f *decFnInfo) kMap(rv reflect.Value) {
  565. containerLen := f.dd.readMapStart()
  566. if rv.IsNil() {
  567. rv.Set(reflect.MakeMap(f.ti.rt))
  568. }
  569. if containerLen == 0 {
  570. f.dd.readMapEnd()
  571. return
  572. }
  573. ktype, vtype := f.ti.rt.Key(), f.ti.rt.Elem()
  574. ktypeId := reflect.ValueOf(ktype).Pointer()
  575. var keyFn, valFn decFn
  576. var xtyp reflect.Type
  577. for xtyp = ktype; xtyp.Kind() == reflect.Ptr; xtyp = xtyp.Elem() {
  578. }
  579. keyFn = f.d.getDecFn(xtyp)
  580. for xtyp = vtype; xtyp.Kind() == reflect.Ptr; xtyp = xtyp.Elem() {
  581. }
  582. valFn = f.d.getDecFn(xtyp)
  583. // for j := 0; j < containerLen; j++ {
  584. for j := 0; ; j++ {
  585. if containerLen >= 0 {
  586. if j >= containerLen {
  587. break
  588. }
  589. } else if f.dd.checkBreak() {
  590. break
  591. }
  592. if j > 0 {
  593. f.dd.readMapEntrySeparator()
  594. }
  595. rvk := reflect.New(ktype).Elem()
  596. f.d.decodeValue(rvk, keyFn)
  597. // special case if a byte array.
  598. if ktypeId == intfTypId {
  599. rvk = rvk.Elem()
  600. if rvk.Type() == uint8SliceTyp {
  601. rvk = reflect.ValueOf(string(rvk.Bytes()))
  602. }
  603. }
  604. rvv := rv.MapIndex(rvk)
  605. if !rvv.IsValid() {
  606. rvv = reflect.New(vtype).Elem()
  607. }
  608. f.dd.readMapKVSeparator()
  609. f.d.decodeValue(rvv, valFn)
  610. rv.SetMapIndex(rvk, rvv)
  611. }
  612. f.dd.readMapEnd()
  613. }
  614. // A Decoder reads and decodes an object from an input stream in the codec format.
  615. type Decoder struct {
  616. r decReader
  617. d decDriver
  618. h *BasicHandle
  619. hh Handle
  620. f map[uintptr]decFn
  621. x []uintptr
  622. s []decFn
  623. b [32]byte
  624. }
  625. // NewDecoder returns a Decoder for decoding a stream of bytes from an io.Reader.
  626. //
  627. // For efficiency, Users are encouraged to pass in a memory buffered reader
  628. // (eg bufio.Reader, bytes.Buffer).
  629. func NewDecoder(r io.Reader, h Handle) *Decoder {
  630. z := ioDecReader{
  631. r: r,
  632. }
  633. z.br, _ = r.(io.ByteScanner)
  634. return &Decoder{r: &z, hh: h, h: h.getBasicHandle(), d: h.newDecDriver(&z)}
  635. }
  636. // NewDecoderBytes returns a Decoder which efficiently decodes directly
  637. // from a byte slice with zero copying.
  638. func NewDecoderBytes(in []byte, h Handle) *Decoder {
  639. z := bytesDecReader{
  640. b: in,
  641. a: len(in),
  642. }
  643. return &Decoder{r: &z, hh: h, h: h.getBasicHandle(), d: h.newDecDriver(&z)}
  644. }
  645. // Decode decodes the stream from reader and stores the result in the
  646. // value pointed to by v. v cannot be a nil pointer. v can also be
  647. // a reflect.Value of a pointer.
  648. //
  649. // Note that a pointer to a nil interface is not a nil pointer.
  650. // If you do not know what type of stream it is, pass in a pointer to a nil interface.
  651. // We will decode and store a value in that nil interface.
  652. //
  653. // Sample usages:
  654. // // Decoding into a non-nil typed value
  655. // var f float32
  656. // err = codec.NewDecoder(r, handle).Decode(&f)
  657. //
  658. // // Decoding into nil interface
  659. // var v interface{}
  660. // dec := codec.NewDecoder(r, handle)
  661. // err = dec.Decode(&v)
  662. //
  663. // When decoding into a nil interface{}, we will decode into an appropriate value based
  664. // on the contents of the stream:
  665. // - Numbers are decoded as float64, int64 or uint64.
  666. // - Other values are decoded appropriately depending on the type:
  667. // bool, string, []byte, time.Time, etc
  668. // - Extensions are decoded as RawExt (if no ext function registered for the tag)
  669. // Configurations exist on the Handle to override defaults
  670. // (e.g. for MapType, SliceType and how to decode raw bytes).
  671. //
  672. // When decoding into a non-nil interface{} value, the mode of encoding is based on the
  673. // type of the value. When a value is seen:
  674. // - If an extension is registered for it, call that extension function
  675. // - If it implements BinaryUnmarshaler, call its UnmarshalBinary(data []byte) error
  676. // - Else decode it based on its reflect.Kind
  677. //
  678. // There are some special rules when decoding into containers (slice/array/map/struct).
  679. // Decode will typically use the stream contents to UPDATE the container.
  680. // - A map can be decoded from a stream map, by updating matching keys.
  681. // - A slice can be decoded from a stream array,
  682. // by updating the first n elements, where n is length of the stream.
  683. // - A slice can be decoded from a stream map, by decoding as if
  684. // it contains a sequence of key-value pairs.
  685. // - A struct can be decoded from a stream map, by updating matching fields.
  686. // - A struct can be decoded from a stream array,
  687. // by updating fields as they occur in the struct (by index).
  688. //
  689. // When decoding a stream map or array with length of 0 into a nil map or slice,
  690. // we reset the destination map or slice to a zero-length value.
  691. //
  692. // However, when decoding a stream nil, we reset the destination container
  693. // to its "zero" value (e.g. nil for slice/map, etc).
  694. //
  695. func (d *Decoder) Decode(v interface{}) (err error) {
  696. defer panicToErr(&err)
  697. d.decode(v)
  698. return
  699. }
  700. // MustDecode is like Decode, but panics if unable to Decode.
  701. // This provides insight to the code location that triggered the error.
  702. func (d *Decoder) MustDecode(v interface{}) {
  703. d.decode(v)
  704. }
  705. func (d *Decoder) decode(iv interface{}) {
  706. d.d.initReadNext()
  707. switch v := iv.(type) {
  708. case nil:
  709. decErr("cannot decode into nil.")
  710. case reflect.Value:
  711. d.chkPtrValue(v)
  712. d.decodeValue(v.Elem(), decFn{})
  713. case *string:
  714. *v = d.d.decodeString()
  715. case *bool:
  716. *v = d.d.decodeBool()
  717. case *int:
  718. *v = int(d.d.decodeInt(intBitsize))
  719. case *int8:
  720. *v = int8(d.d.decodeInt(8))
  721. case *int16:
  722. *v = int16(d.d.decodeInt(16))
  723. case *int32:
  724. *v = int32(d.d.decodeInt(32))
  725. case *int64:
  726. *v = d.d.decodeInt(64)
  727. case *uint:
  728. *v = uint(d.d.decodeUint(uintBitsize))
  729. case *uint8:
  730. *v = uint8(d.d.decodeUint(8))
  731. case *uint16:
  732. *v = uint16(d.d.decodeUint(16))
  733. case *uint32:
  734. *v = uint32(d.d.decodeUint(32))
  735. case *uint64:
  736. *v = d.d.decodeUint(64)
  737. case *float32:
  738. *v = float32(d.d.decodeFloat(true))
  739. case *float64:
  740. *v = d.d.decodeFloat(false)
  741. case *[]uint8:
  742. *v, _ = d.d.decodeBytes(*v)
  743. case *interface{}:
  744. d.decodeValue(reflect.ValueOf(iv).Elem(), decFn{})
  745. default:
  746. rv := reflect.ValueOf(iv)
  747. d.chkPtrValue(rv)
  748. d.decodeValue(rv.Elem(), decFn{})
  749. }
  750. }
  751. func (d *Decoder) decodeValue(rv reflect.Value, fn decFn) {
  752. d.d.initReadNext()
  753. if d.d.tryDecodeAsNil() {
  754. // If value in stream is nil, set the dereferenced value to its "zero" value (if settable)
  755. if rv.Kind() == reflect.Ptr {
  756. if !rv.IsNil() {
  757. rv.Set(reflect.Zero(rv.Type()))
  758. }
  759. return
  760. }
  761. // for rv.Kind() == reflect.Ptr {
  762. // rv = rv.Elem()
  763. // }
  764. if rv.IsValid() { // rv.CanSet() // always settable, except it's invalid
  765. rv.Set(reflect.Zero(rv.Type()))
  766. }
  767. return
  768. }
  769. // If stream is not containing a nil value, then we can deref to the base
  770. // non-pointer value, and decode into that.
  771. for rv.Kind() == reflect.Ptr {
  772. if rv.IsNil() {
  773. rv.Set(reflect.New(rv.Type().Elem()))
  774. }
  775. rv = rv.Elem()
  776. }
  777. if fn.i == nil {
  778. fn = d.getDecFn(rv.Type())
  779. }
  780. fn.f(fn.i, rv)
  781. return
  782. }
  783. func (d *Decoder) getDecFn(rt reflect.Type) (fn decFn) {
  784. rtid := reflect.ValueOf(rt).Pointer()
  785. // retrieve or register a focus'ed function for this type
  786. // to eliminate need to do the retrieval multiple times
  787. // if d.f == nil && d.s == nil { debugf("---->Creating new dec f map for type: %v\n", rt) }
  788. var ok bool
  789. if useMapForCodecCache {
  790. fn, ok = d.f[rtid]
  791. } else {
  792. for i, v := range d.x {
  793. if v == rtid {
  794. fn, ok = d.s[i], true
  795. break
  796. }
  797. }
  798. }
  799. if !ok {
  800. // debugf("\tCreating new dec fn for type: %v\n", rt)
  801. fi := decFnInfo{ti: getTypeInfo(rtid, rt), d: d, dd: d.d}
  802. fn.i = &fi
  803. // An extension can be registered for any type, regardless of the Kind
  804. // (e.g. type BitSet int64, type MyStruct { / * unexported fields * / }, type X []int, etc.
  805. //
  806. // We can't check if it's an extension byte here first, because the user may have
  807. // registered a pointer or non-pointer type, meaning we may have to recurse first
  808. // before matching a mapped type, even though the extension byte is already detected.
  809. //
  810. // NOTE: if decoding into a nil interface{}, we return a non-nil
  811. // value except even if the container registers a length of 0.
  812. if rtid == rawExtTypId {
  813. fn.f = (*decFnInfo).rawExt
  814. } else if d.d.isBuiltinType(rtid) {
  815. fn.f = (*decFnInfo).builtin
  816. } else if xfFn := d.h.getExt(rtid); xfFn != nil {
  817. fi.xfTag, fi.xfFn = xfFn.tag, xfFn.ext
  818. fn.f = (*decFnInfo).ext
  819. } else if supportMarshalInterfaces && d.hh.isBinaryEncoding() && fi.ti.bunm {
  820. fn.f = (*decFnInfo).binaryUnmarshal
  821. } else if supportMarshalInterfaces && !d.hh.isBinaryEncoding() && fi.ti.tunm {
  822. fn.f = (*decFnInfo).textUnmarshal
  823. } else {
  824. rk := rt.Kind()
  825. if fastpathEnabled && (rk == reflect.Map || rk == reflect.Slice) {
  826. if fn.f, ok = fastpathsDec[rtid]; !ok && rt.PkgPath() != "" {
  827. // use mapping for underlying type if there
  828. var rtu reflect.Type
  829. if rk == reflect.Map {
  830. rtu = reflect.MapOf(rt.Key(), rt.Elem())
  831. } else {
  832. rtu = reflect.SliceOf(rt.Elem())
  833. }
  834. rtuid := reflect.ValueOf(rtu).Pointer()
  835. if fn.f, ok = fastpathsDec[rtuid]; ok {
  836. xfnf := fn.f
  837. xrt := fastpathsTyp[rtuid]
  838. fn.f = func(xf *decFnInfo, xrv reflect.Value) {
  839. // xfnf(xf, xrv.Convert(xrt))
  840. xfnf(xf, xrv.Addr().Convert(reflect.PtrTo(xrt)).Elem())
  841. }
  842. }
  843. }
  844. }
  845. if fn.f == nil {
  846. switch rk {
  847. case reflect.String:
  848. fn.f = (*decFnInfo).kString
  849. case reflect.Bool:
  850. fn.f = (*decFnInfo).kBool
  851. case reflect.Int:
  852. fn.f = (*decFnInfo).kInt
  853. case reflect.Int64:
  854. fn.f = (*decFnInfo).kInt64
  855. case reflect.Int32:
  856. fn.f = (*decFnInfo).kInt32
  857. case reflect.Int8:
  858. fn.f = (*decFnInfo).kInt8
  859. case reflect.Int16:
  860. fn.f = (*decFnInfo).kInt16
  861. case reflect.Float32:
  862. fn.f = (*decFnInfo).kFloat32
  863. case reflect.Float64:
  864. fn.f = (*decFnInfo).kFloat64
  865. case reflect.Uint8:
  866. fn.f = (*decFnInfo).kUint8
  867. case reflect.Uint64:
  868. fn.f = (*decFnInfo).kUint64
  869. case reflect.Uint:
  870. fn.f = (*decFnInfo).kUint
  871. case reflect.Uint32:
  872. fn.f = (*decFnInfo).kUint32
  873. case reflect.Uint16:
  874. fn.f = (*decFnInfo).kUint16
  875. // case reflect.Ptr:
  876. // fn.f = (*decFnInfo).kPtr
  877. case reflect.Interface:
  878. fn.f = (*decFnInfo).kInterface
  879. case reflect.Struct:
  880. fn.f = (*decFnInfo).kStruct
  881. case reflect.Slice:
  882. fn.f = (*decFnInfo).kSlice
  883. case reflect.Array:
  884. fi.array = true
  885. fn.f = (*decFnInfo).kArray
  886. case reflect.Map:
  887. fn.f = (*decFnInfo).kMap
  888. default:
  889. fn.f = (*decFnInfo).kErr
  890. }
  891. }
  892. }
  893. if useMapForCodecCache {
  894. if d.f == nil {
  895. d.f = make(map[uintptr]decFn, 16)
  896. }
  897. d.f[rtid] = fn
  898. } else {
  899. d.s = append(d.s, fn)
  900. d.x = append(d.x, rtid)
  901. }
  902. }
  903. return
  904. }
  905. func (d *Decoder) chkPtrValue(rv reflect.Value) {
  906. // We can only decode into a non-nil pointer
  907. if rv.Kind() == reflect.Ptr && !rv.IsNil() {
  908. return
  909. }
  910. if !rv.IsValid() {
  911. decErr("cannot decode into a zero (ie invalid) reflect.Value")
  912. }
  913. if !rv.CanInterface() {
  914. decErr("cannot decode into a value without an interface: %v", rv)
  915. }
  916. rvi := rv.Interface()
  917. decErr("cannot decode into non-pointer or nil pointer. Got: %v, %T, %v",
  918. rv.Kind(), rvi, rvi)
  919. }
  920. func (d *Decoder) decEmbeddedField(rv reflect.Value, index []int) {
  921. // d.decodeValue(rv.FieldByIndex(index))
  922. // nil pointers may be here; so reproduce FieldByIndex logic + enhancements
  923. for _, j := range index {
  924. if rv.Kind() == reflect.Ptr {
  925. if rv.IsNil() {
  926. rv.Set(reflect.New(rv.Type().Elem()))
  927. }
  928. // If a pointer, it must be a pointer to struct (based on typeInfo contract)
  929. rv = rv.Elem()
  930. }
  931. rv = rv.Field(j)
  932. }
  933. d.decodeValue(rv, decFn{})
  934. }
  935. // --------------------------------------------------
  936. // decSliceHelper assists when decoding into a slice, from a map or an array in the stream.
  937. // A slice can be set from a map or array in stream. This supports the MapBySlice interface.
  938. type decSliceHelper struct {
  939. dd decDriver
  940. ct valueType
  941. }
  942. func (x *decSliceHelper) start() (sliceLen int) {
  943. if x.dd.isContainerType(valueTypeArray) {
  944. x.ct = valueTypeArray
  945. return x.dd.readArrayStart()
  946. }
  947. if x.dd.isContainerType(valueTypeMap) {
  948. x.ct = valueTypeMap
  949. return x.dd.readMapStart() * 2
  950. }
  951. decErr("only encoded map or array can be decoded into a slice")
  952. panic("unreachable")
  953. }
  954. func (x *decSliceHelper) sep(index int) {
  955. if x.ct == valueTypeArray {
  956. x.dd.readArrayEntrySeparator()
  957. } else {
  958. if index%2 == 0 {
  959. x.dd.readMapEntrySeparator()
  960. } else {
  961. x.dd.readMapKVSeparator()
  962. }
  963. }
  964. }
  965. func (x *decSliceHelper) end() {
  966. if x.ct == valueTypeArray {
  967. x.dd.readArrayEnd()
  968. } else {
  969. x.dd.readMapEnd()
  970. }
  971. }
  972. func decErr(format string, params ...interface{}) {
  973. doPanic(msgTagDec, format, params...)
  974. }