decode.go 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350
  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. package codec
  4. import (
  5. "encoding"
  6. "errors"
  7. "io"
  8. "math"
  9. "reflect"
  10. "strconv"
  11. "time"
  12. )
  13. const msgBadDesc = "unrecognized descriptor byte"
  14. const (
  15. decDefMaxDepth = 1024 // maximum depth
  16. decDefChanCap = 64 // should be large, as cap cannot be expanded
  17. decScratchByteArrayLen = (8 + 2 + 2) * 8 // around cacheLineSize ie ~64, depending on Decoder size
  18. // MARKER: massage decScratchByteArrayLen to ensure xxxDecDriver structs fit within cacheLine*N
  19. // decFailNonEmptyIntf configures whether we error
  20. // when decoding naked into a non-empty interface.
  21. //
  22. // Typically, we cannot decode non-nil stream value into
  23. // nil interface with methods (e.g. io.Reader).
  24. // However, in some scenarios, this should be allowed:
  25. // - MapType
  26. // - SliceType
  27. // - Extensions
  28. //
  29. // Consequently, we should relax this. Put it behind a const flag for now.
  30. decFailNonEmptyIntf = false
  31. // decUseTransient says that we should not use the transient optimization.
  32. //
  33. // There's potential for GC corruption or memory overwrites if transient isn't
  34. // used carefully, so this flag helps turn it off quickly if needed.
  35. //
  36. // Use it everywhere needed so we can completely remove unused code blocks.
  37. decUseTransient = true
  38. )
  39. var (
  40. errNeedMapOrArrayDecodeToStruct = errors.New("only encoded map or array can decode into struct")
  41. errCannotDecodeIntoNil = errors.New("cannot decode into nil")
  42. errExpandSliceCannotChange = errors.New("expand slice: cannot change")
  43. errDecoderNotInitialized = errors.New("Decoder not initialized")
  44. errDecUnreadByteNothingToRead = errors.New("cannot unread - nothing has been read")
  45. errDecUnreadByteLastByteNotRead = errors.New("cannot unread - last byte has not been read")
  46. errDecUnreadByteUnknown = errors.New("cannot unread - reason unknown")
  47. errMaxDepthExceeded = errors.New("maximum decoding depth exceeded")
  48. )
  49. // decByteState tracks where the []byte returned by the last call
  50. // to DecodeBytes or DecodeStringAsByte came from
  51. type decByteState uint8
  52. const (
  53. decByteStateNone decByteState = iota
  54. decByteStateZerocopy // view into []byte that we are decoding from
  55. decByteStateReuseBuf // view into transient buffer used internally by decDriver
  56. // decByteStateNewAlloc
  57. )
  58. type decNotDecodeableReason uint8
  59. const (
  60. decNotDecodeableReasonUnknown decNotDecodeableReason = iota
  61. decNotDecodeableReasonBadKind
  62. decNotDecodeableReasonNonAddrValue
  63. decNotDecodeableReasonNilReference
  64. )
  65. type decDriver interface {
  66. // this will check if the next token is a break.
  67. CheckBreak() bool
  68. // TryNil tries to decode as nil.
  69. // If a nil is in the stream, it consumes it and returns true.
  70. //
  71. // Note: if TryNil returns true, that must be handled.
  72. TryNil() bool
  73. // ContainerType returns one of: Bytes, String, Nil, Slice or Map.
  74. //
  75. // Return unSet if not known.
  76. //
  77. // Note: Implementations MUST fully consume sentinel container types, specifically Nil.
  78. ContainerType() (vt valueType)
  79. // DecodeNaked will decode primitives (number, bool, string, []byte) and RawExt.
  80. // For maps and arrays, it will not do the decoding in-band, but will signal
  81. // the decoder, so that is done later, by setting the fauxUnion.valueType field.
  82. //
  83. // Note: Numbers are decoded as int64, uint64, float64 only (no smaller sized number types).
  84. // for extensions, DecodeNaked must read the tag and the []byte if it exists.
  85. // if the []byte is not read, then kInterfaceNaked will treat it as a Handle
  86. // that stores the subsequent value in-band, and complete reading the RawExt.
  87. //
  88. // extensions should also use readx to decode them, for efficiency.
  89. // kInterface will extract the detached byte slice if it has to pass it outside its realm.
  90. DecodeNaked()
  91. DecodeInt64() (i int64)
  92. DecodeUint64() (ui uint64)
  93. DecodeFloat64() (f float64)
  94. DecodeBool() (b bool)
  95. // DecodeStringAsBytes returns the bytes representing a string.
  96. // It will return a view into scratch buffer or input []byte (if applicable).
  97. //
  98. // Note: This can also decode symbols, if supported.
  99. //
  100. // Users should consume it right away and not store it for later use.
  101. DecodeStringAsBytes() (v []byte)
  102. // DecodeBytes returns the bytes representing a binary value.
  103. // It will return a view into scratch buffer or input []byte (if applicable).
  104. //
  105. // All implementations must honor the contract below:
  106. // if ZeroCopy and applicable, return a view into input []byte we are decoding from
  107. // else if in == nil, return a view into scratch buffer
  108. // else append decoded value to in[:0] and return that
  109. // (this can be simulated by passing []byte{} as in parameter)
  110. //
  111. // Implementations must also update Decoder.decByteState on each call to
  112. // DecodeBytes or DecodeStringAsBytes. Some callers may check that and work appropriately.
  113. //
  114. // Note: DecodeBytes may decode past the length of the passed byte slice, up to the cap.
  115. // Consequently, it is ok to pass a zero-len slice to DecodeBytes, as the returned
  116. // byte slice will have the appropriate length.
  117. DecodeBytes(in []byte) (out []byte)
  118. // DecodeBytes(bs []byte, isstring, zerocopy bool) (bsOut []byte)
  119. // DecodeExt will decode into a *RawExt or into an extension.
  120. DecodeExt(v interface{}, basetype reflect.Type, xtag uint64, ext Ext)
  121. // decodeExt(verifyTag bool, tag byte) (xtag byte, xbs []byte)
  122. DecodeTime() (t time.Time)
  123. // ReadArrayStart will return the length of the array.
  124. // If the format doesn't prefix the length, it returns containerLenUnknown.
  125. // If the expected array was a nil in the stream, it returns containerLenNil.
  126. ReadArrayStart() int
  127. ReadArrayEnd()
  128. // ReadMapStart will return the length of the array.
  129. // If the format doesn't prefix the length, it returns containerLenUnknown.
  130. // If the expected array was a nil in the stream, it returns containerLenNil.
  131. ReadMapStart() int
  132. ReadMapEnd()
  133. reset()
  134. // atEndOfDecode()
  135. // nextValueBytes will return the bytes representing the next value in the stream.
  136. //
  137. // if start is nil, then treat it as a request to discard the next set of bytes,
  138. // and the return response does not matter.
  139. // Typically, this means that the returned []byte is nil/empty/undefined.
  140. //
  141. // Optimize for decoding from a []byte, where the nextValueBytes will just be a sub-slice
  142. // of the input slice. Callers that need to use this to not be a view into the input bytes
  143. // should handle it appropriately.
  144. nextValueBytes(start []byte) []byte
  145. // descBd will describe the token descriptor that signifies what type was decoded
  146. descBd() string
  147. decoder() *Decoder
  148. driverStateManager
  149. decNegintPosintFloatNumber
  150. }
  151. type decDriverContainerTracker interface {
  152. ReadArrayElem()
  153. ReadMapElemKey()
  154. ReadMapElemValue()
  155. }
  156. type decNegintPosintFloatNumber interface {
  157. decInteger() (ui uint64, neg, ok bool)
  158. decFloat() (f float64, ok bool)
  159. }
  160. type decDriverNoopNumberHelper struct{}
  161. func (x decDriverNoopNumberHelper) decInteger() (ui uint64, neg, ok bool) {
  162. panic("decInteger unsupported")
  163. }
  164. func (x decDriverNoopNumberHelper) decFloat() (f float64, ok bool) { panic("decFloat unsupported") }
  165. type decDriverNoopContainerReader struct{}
  166. func (x decDriverNoopContainerReader) ReadArrayStart() (v int) { panic("ReadArrayStart unsupported") }
  167. func (x decDriverNoopContainerReader) ReadArrayEnd() {}
  168. func (x decDriverNoopContainerReader) ReadMapStart() (v int) { panic("ReadMapStart unsupported") }
  169. func (x decDriverNoopContainerReader) ReadMapEnd() {}
  170. func (x decDriverNoopContainerReader) CheckBreak() (v bool) { return }
  171. // DecodeOptions captures configuration options during decode.
  172. type DecodeOptions struct {
  173. // MapType specifies type to use during schema-less decoding of a map in the stream.
  174. // If nil (unset), we default to map[string]interface{} iff json handle and MapKeyAsString=true,
  175. // else map[interface{}]interface{}.
  176. MapType reflect.Type
  177. // SliceType specifies type to use during schema-less decoding of an array in the stream.
  178. // If nil (unset), we default to []interface{} for all formats.
  179. SliceType reflect.Type
  180. // MaxInitLen defines the maxinum initial length that we "make" a collection
  181. // (string, slice, map, chan). If 0 or negative, we default to a sensible value
  182. // based on the size of an element in the collection.
  183. //
  184. // For example, when decoding, a stream may say that it has 2^64 elements.
  185. // We should not auto-matically provision a slice of that size, to prevent Out-Of-Memory crash.
  186. // Instead, we provision up to MaxInitLen, fill that up, and start appending after that.
  187. MaxInitLen int
  188. // ReaderBufferSize is the size of the buffer used when reading.
  189. //
  190. // if > 0, we use a smart buffer internally for performance purposes.
  191. ReaderBufferSize int
  192. // MaxDepth defines the maximum depth when decoding nested
  193. // maps and slices. If 0 or negative, we default to a suitably large number (currently 1024).
  194. MaxDepth int16
  195. // If ErrorIfNoField, return an error when decoding a map
  196. // from a codec stream into a struct, and no matching struct field is found.
  197. ErrorIfNoField bool
  198. // If ErrorIfNoArrayExpand, return an error when decoding a slice/array that cannot be expanded.
  199. // For example, the stream contains an array of 8 items, but you are decoding into a [4]T array,
  200. // or you are decoding into a slice of length 4 which is non-addressable (and so cannot be set).
  201. ErrorIfNoArrayExpand bool
  202. // If SignedInteger, use the int64 during schema-less decoding of unsigned values (not uint64).
  203. SignedInteger bool
  204. // MapValueReset controls how we decode into a map value.
  205. //
  206. // By default, we MAY retrieve the mapping for a key, and then decode into that.
  207. // However, especially with big maps, that retrieval may be expensive and unnecessary
  208. // if the stream already contains all that is necessary to recreate the value.
  209. //
  210. // If true, we will never retrieve the previous mapping,
  211. // but rather decode into a new value and set that in the map.
  212. //
  213. // If false, we will retrieve the previous mapping if necessary e.g.
  214. // the previous mapping is a pointer, or is a struct or array with pre-set state,
  215. // or is an interface.
  216. MapValueReset bool
  217. // SliceElementReset: on decoding a slice, reset the element to a zero value first.
  218. //
  219. // concern: if the slice already contained some garbage, we will decode into that garbage.
  220. SliceElementReset bool
  221. // InterfaceReset controls how we decode into an interface.
  222. //
  223. // By default, when we see a field that is an interface{...},
  224. // or a map with interface{...} value, we will attempt decoding into the
  225. // "contained" value.
  226. //
  227. // However, this prevents us from reading a string into an interface{}
  228. // that formerly contained a number.
  229. //
  230. // If true, we will decode into a new "blank" value, and set that in the interface.
  231. // If false, we will decode into whatever is contained in the interface.
  232. InterfaceReset bool
  233. // InternString controls interning of strings during decoding.
  234. //
  235. // Some handles, e.g. json, typically will read map keys as strings.
  236. // If the set of keys are finite, it may help reduce allocation to
  237. // look them up from a map (than to allocate them afresh).
  238. //
  239. // Note: Handles will be smart when using the intern functionality.
  240. // Every string should not be interned.
  241. // An excellent use-case for interning is struct field names,
  242. // or map keys where key type is string.
  243. InternString bool
  244. // PreferArrayOverSlice controls whether to decode to an array or a slice.
  245. //
  246. // This only impacts decoding into a nil interface{}.
  247. //
  248. // Consequently, it has no effect on codecgen.
  249. //
  250. // *Note*: This only applies if using go1.5 and above,
  251. // as it requires reflect.ArrayOf support which was absent before go1.5.
  252. PreferArrayOverSlice bool
  253. // DeleteOnNilMapValue controls how to decode a nil value in the stream.
  254. //
  255. // If true, we will delete the mapping of the key.
  256. // Else, just set the mapping to the zero value of the type.
  257. //
  258. // Deprecated: This does NOTHING and is left behind for compiling compatibility.
  259. // This change is necessitated because 'nil' in a stream now consistently
  260. // means the zero value (ie reset the value to its zero state).
  261. DeleteOnNilMapValue bool
  262. // RawToString controls how raw bytes in a stream are decoded into a nil interface{}.
  263. // By default, they are decoded as []byte, but can be decoded as string (if configured).
  264. RawToString bool
  265. // ZeroCopy controls whether decoded values of []byte or string type
  266. // point into the input []byte parameter passed to a NewDecoderBytes/ResetBytes(...) call.
  267. //
  268. // To illustrate, if ZeroCopy and decoding from a []byte (not io.Writer),
  269. // then a []byte or string in the output result may just be a slice of (point into)
  270. // the input bytes.
  271. //
  272. // This optimization prevents unnecessary copying.
  273. //
  274. // However, it is made optional, as the caller MUST ensure that the input parameter []byte is
  275. // not modified after the Decode() happens, as any changes are mirrored in the decoded result.
  276. ZeroCopy bool
  277. // PreferPointerForStructOrArray controls whether a struct or array
  278. // is stored in a nil interface{}, or a pointer to it.
  279. //
  280. // This mostly impacts when we decode registered extensions.
  281. PreferPointerForStructOrArray bool
  282. }
  283. // ----------------------------------------
  284. func (d *Decoder) rawExt(f *codecFnInfo, rv reflect.Value) {
  285. d.d.DecodeExt(rv2i(rv), f.ti.rt, 0, nil)
  286. }
  287. func (d *Decoder) ext(f *codecFnInfo, rv reflect.Value) {
  288. d.d.DecodeExt(rv2i(rv), f.ti.rt, f.xfTag, f.xfFn)
  289. }
  290. func (d *Decoder) selferUnmarshal(f *codecFnInfo, rv reflect.Value) {
  291. rv2i(rv).(Selfer).CodecDecodeSelf(d)
  292. }
  293. func (d *Decoder) binaryUnmarshal(f *codecFnInfo, rv reflect.Value) {
  294. bm := rv2i(rv).(encoding.BinaryUnmarshaler)
  295. xbs := d.d.DecodeBytes(nil)
  296. fnerr := bm.UnmarshalBinary(xbs)
  297. d.onerror(fnerr)
  298. }
  299. func (d *Decoder) textUnmarshal(f *codecFnInfo, rv reflect.Value) {
  300. tm := rv2i(rv).(encoding.TextUnmarshaler)
  301. fnerr := tm.UnmarshalText(d.d.DecodeStringAsBytes())
  302. d.onerror(fnerr)
  303. }
  304. func (d *Decoder) jsonUnmarshal(f *codecFnInfo, rv reflect.Value) {
  305. d.jsonUnmarshalV(rv2i(rv).(jsonUnmarshaler))
  306. }
  307. func (d *Decoder) jsonUnmarshalV(tm jsonUnmarshaler) {
  308. // grab the bytes to be read, as UnmarshalJSON needs the full JSON so as to unmarshal it itself.
  309. var bs0 = []byte{}
  310. if !d.bytes {
  311. bs0 = d.blist.get(256)
  312. }
  313. bs := d.d.nextValueBytes(bs0)
  314. fnerr := tm.UnmarshalJSON(bs)
  315. if !d.bytes {
  316. d.blist.put(bs)
  317. if !byteSliceSameData(bs0, bs) {
  318. d.blist.put(bs0)
  319. }
  320. }
  321. d.onerror(fnerr)
  322. }
  323. func (d *Decoder) kErr(f *codecFnInfo, rv reflect.Value) {
  324. d.errorf("no decoding function defined for kind %v", rv.Kind())
  325. }
  326. func (d *Decoder) raw(f *codecFnInfo, rv reflect.Value) {
  327. rvSetBytes(rv, d.rawBytes())
  328. }
  329. func (d *Decoder) kString(f *codecFnInfo, rv reflect.Value) {
  330. rvSetString(rv, d.stringZC(d.d.DecodeStringAsBytes()))
  331. }
  332. func (d *Decoder) kBool(f *codecFnInfo, rv reflect.Value) {
  333. rvSetBool(rv, d.d.DecodeBool())
  334. }
  335. func (d *Decoder) kTime(f *codecFnInfo, rv reflect.Value) {
  336. rvSetTime(rv, d.d.DecodeTime())
  337. }
  338. func (d *Decoder) kFloat32(f *codecFnInfo, rv reflect.Value) {
  339. rvSetFloat32(rv, d.decodeFloat32())
  340. }
  341. func (d *Decoder) kFloat64(f *codecFnInfo, rv reflect.Value) {
  342. rvSetFloat64(rv, d.d.DecodeFloat64())
  343. }
  344. func (d *Decoder) kComplex64(f *codecFnInfo, rv reflect.Value) {
  345. rvSetComplex64(rv, complex(d.decodeFloat32(), 0))
  346. }
  347. func (d *Decoder) kComplex128(f *codecFnInfo, rv reflect.Value) {
  348. rvSetComplex128(rv, complex(d.d.DecodeFloat64(), 0))
  349. }
  350. func (d *Decoder) kInt(f *codecFnInfo, rv reflect.Value) {
  351. rvSetInt(rv, int(chkOvf.IntV(d.d.DecodeInt64(), intBitsize)))
  352. }
  353. func (d *Decoder) kInt8(f *codecFnInfo, rv reflect.Value) {
  354. rvSetInt8(rv, int8(chkOvf.IntV(d.d.DecodeInt64(), 8)))
  355. }
  356. func (d *Decoder) kInt16(f *codecFnInfo, rv reflect.Value) {
  357. rvSetInt16(rv, int16(chkOvf.IntV(d.d.DecodeInt64(), 16)))
  358. }
  359. func (d *Decoder) kInt32(f *codecFnInfo, rv reflect.Value) {
  360. rvSetInt32(rv, int32(chkOvf.IntV(d.d.DecodeInt64(), 32)))
  361. }
  362. func (d *Decoder) kInt64(f *codecFnInfo, rv reflect.Value) {
  363. rvSetInt64(rv, d.d.DecodeInt64())
  364. }
  365. func (d *Decoder) kUint(f *codecFnInfo, rv reflect.Value) {
  366. rvSetUint(rv, uint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize)))
  367. }
  368. func (d *Decoder) kUintptr(f *codecFnInfo, rv reflect.Value) {
  369. rvSetUintptr(rv, uintptr(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize)))
  370. }
  371. func (d *Decoder) kUint8(f *codecFnInfo, rv reflect.Value) {
  372. rvSetUint8(rv, uint8(chkOvf.UintV(d.d.DecodeUint64(), 8)))
  373. }
  374. func (d *Decoder) kUint16(f *codecFnInfo, rv reflect.Value) {
  375. rvSetUint16(rv, uint16(chkOvf.UintV(d.d.DecodeUint64(), 16)))
  376. }
  377. func (d *Decoder) kUint32(f *codecFnInfo, rv reflect.Value) {
  378. rvSetUint32(rv, uint32(chkOvf.UintV(d.d.DecodeUint64(), 32)))
  379. }
  380. func (d *Decoder) kUint64(f *codecFnInfo, rv reflect.Value) {
  381. rvSetUint64(rv, d.d.DecodeUint64())
  382. }
  383. func (d *Decoder) kInterfaceNaked(f *codecFnInfo) (rvn reflect.Value) {
  384. // nil interface:
  385. // use some hieristics to decode it appropriately
  386. // based on the detected next value in the stream.
  387. n := d.naked()
  388. d.d.DecodeNaked()
  389. // We cannot decode non-nil stream value into nil interface with methods (e.g. io.Reader).
  390. // Howver, it is possible that the user has ways to pass in a type for a given interface
  391. // - MapType
  392. // - SliceType
  393. // - Extensions
  394. //
  395. // Consequently, we should relax this. Put it behind a const flag for now.
  396. if decFailNonEmptyIntf && f.ti.numMeth > 0 {
  397. d.errorf("cannot decode non-nil codec value into nil %v (%v methods)", f.ti.rt, f.ti.numMeth)
  398. }
  399. switch n.v {
  400. case valueTypeMap:
  401. mtid := d.mtid
  402. if mtid == 0 {
  403. if d.jsms { // if json, default to a map type with string keys
  404. mtid = mapStrIntfTypId // for json performance
  405. } else {
  406. mtid = mapIntfIntfTypId
  407. }
  408. }
  409. if mtid == mapStrIntfTypId {
  410. var v2 map[string]interface{}
  411. d.decode(&v2)
  412. rvn = rv4iptr(&v2).Elem()
  413. } else if mtid == mapIntfIntfTypId {
  414. var v2 map[interface{}]interface{}
  415. d.decode(&v2)
  416. rvn = rv4iptr(&v2).Elem()
  417. } else if d.mtr {
  418. rvn = reflect.New(d.h.MapType)
  419. d.decode(rv2i(rvn))
  420. rvn = rvn.Elem()
  421. } else {
  422. rvn = rvZeroAddrK(d.h.MapType, reflect.Map)
  423. d.decodeValue(rvn, nil)
  424. }
  425. case valueTypeArray:
  426. if d.stid == 0 || d.stid == intfSliceTypId {
  427. var v2 []interface{}
  428. d.decode(&v2)
  429. rvn = rv4iptr(&v2).Elem()
  430. } else if d.str {
  431. rvn = reflect.New(d.h.SliceType)
  432. d.decode(rv2i(rvn))
  433. rvn = rvn.Elem()
  434. } else {
  435. rvn = rvZeroAddrK(d.h.SliceType, reflect.Slice)
  436. d.decodeValue(rvn, nil)
  437. }
  438. if reflectArrayOfSupported && d.h.PreferArrayOverSlice {
  439. rvn = rvGetArray4Slice(rvn)
  440. }
  441. case valueTypeExt:
  442. tag, bytes := n.u, n.l // calling decode below might taint the values
  443. bfn := d.h.getExtForTag(tag)
  444. var re = RawExt{Tag: tag}
  445. if bytes == nil {
  446. // it is one of the InterfaceExt ones: json and cbor.
  447. // most likely cbor, as json decoding never reveals valueTypeExt (no tagging support)
  448. if bfn == nil {
  449. d.decode(&re.Value)
  450. rvn = rv4iptr(&re).Elem()
  451. } else {
  452. if bfn.ext == SelfExt {
  453. rvn = rvZeroAddrK(bfn.rt, bfn.rt.Kind())
  454. d.decodeValue(rvn, d.h.fnNoExt(bfn.rt))
  455. } else {
  456. rvn = reflect.New(bfn.rt)
  457. d.interfaceExtConvertAndDecode(rv2i(rvn), bfn.ext)
  458. rvn = rvn.Elem()
  459. }
  460. }
  461. } else {
  462. // one of the BytesExt ones: binc, msgpack, simple
  463. if bfn == nil {
  464. re.setData(bytes, false)
  465. rvn = rv4iptr(&re).Elem()
  466. } else {
  467. rvn = reflect.New(bfn.rt)
  468. if bfn.ext == SelfExt {
  469. d.sideDecode(rv2i(rvn), bfn.rt, bytes)
  470. } else {
  471. bfn.ext.ReadExt(rv2i(rvn), bytes)
  472. }
  473. rvn = rvn.Elem()
  474. }
  475. }
  476. // if struct/array, directly store pointer into the interface
  477. if d.h.PreferPointerForStructOrArray && rvn.CanAddr() {
  478. if rk := rvn.Kind(); rk == reflect.Array || rk == reflect.Struct {
  479. rvn = rvn.Addr()
  480. }
  481. }
  482. case valueTypeNil:
  483. // rvn = reflect.Zero(f.ti.rt)
  484. // no-op
  485. case valueTypeInt:
  486. rvn = n.ri()
  487. case valueTypeUint:
  488. rvn = n.ru()
  489. case valueTypeFloat:
  490. rvn = n.rf()
  491. case valueTypeBool:
  492. rvn = n.rb()
  493. case valueTypeString, valueTypeSymbol:
  494. rvn = n.rs()
  495. case valueTypeBytes:
  496. rvn = n.rl()
  497. case valueTypeTime:
  498. rvn = n.rt()
  499. default:
  500. halt.errorf("kInterfaceNaked: unexpected valueType: %d", n.v)
  501. }
  502. return
  503. }
  504. func (d *Decoder) kInterface(f *codecFnInfo, rv reflect.Value) {
  505. // Note: A consequence of how kInterface works, is that
  506. // if an interface already contains something, we try
  507. // to decode into what was there before.
  508. // We do not replace with a generic value (as got from decodeNaked).
  509. //
  510. // every interface passed here MUST be settable.
  511. //
  512. // ensure you call rvSetIntf(...) before returning.
  513. isnilrv := rvIsNil(rv)
  514. var rvn reflect.Value
  515. if d.h.InterfaceReset {
  516. // check if mapping to a type: if so, initialize it and move on
  517. rvn = d.h.intf2impl(f.ti.rtid)
  518. if !rvn.IsValid() {
  519. rvn = d.kInterfaceNaked(f)
  520. if rvn.IsValid() {
  521. rvSetIntf(rv, rvn)
  522. } else if !isnilrv {
  523. decSetNonNilRV2Zero4Intf(rv)
  524. }
  525. return
  526. }
  527. } else if isnilrv {
  528. // check if mapping to a type: if so, initialize it and move on
  529. rvn = d.h.intf2impl(f.ti.rtid)
  530. if !rvn.IsValid() {
  531. rvn = d.kInterfaceNaked(f)
  532. if rvn.IsValid() {
  533. rvSetIntf(rv, rvn)
  534. }
  535. return
  536. }
  537. } else {
  538. // now we have a non-nil interface value, meaning it contains a type
  539. rvn = rv.Elem()
  540. }
  541. // rvn is now a non-interface type
  542. canDecode, _ := isDecodeable(rvn)
  543. // Note: interface{} is settable, but underlying type may not be.
  544. // Consequently, we MAY have to allocate a value (containing the underlying value),
  545. // decode into it, and reset the interface to that new value.
  546. if !canDecode {
  547. rvn2 := d.oneShotAddrRV(rvType(rvn), rvn.Kind())
  548. rvSetDirect(rvn2, rvn)
  549. rvn = rvn2
  550. }
  551. d.decodeValue(rvn, nil)
  552. rvSetIntf(rv, rvn)
  553. }
  554. func decStructFieldKeyNotString(dd decDriver, keyType valueType, b *[decScratchByteArrayLen]byte) (rvkencname []byte) {
  555. if keyType == valueTypeInt {
  556. rvkencname = strconv.AppendInt(b[:0], dd.DecodeInt64(), 10)
  557. } else if keyType == valueTypeUint {
  558. rvkencname = strconv.AppendUint(b[:0], dd.DecodeUint64(), 10)
  559. } else if keyType == valueTypeFloat {
  560. rvkencname = strconv.AppendFloat(b[:0], dd.DecodeFloat64(), 'f', -1, 64)
  561. } else {
  562. halt.errorf("invalid struct key type: %v", keyType)
  563. }
  564. return
  565. }
  566. func (d *Decoder) kStruct(f *codecFnInfo, rv reflect.Value) {
  567. ctyp := d.d.ContainerType()
  568. ti := f.ti
  569. var mf MissingFielder
  570. if ti.flagMissingFielder {
  571. mf = rv2i(rv).(MissingFielder)
  572. } else if ti.flagMissingFielderPtr {
  573. mf = rv2i(rvAddr(rv, ti.ptr)).(MissingFielder)
  574. }
  575. if ctyp == valueTypeMap {
  576. containerLen := d.mapStart(d.d.ReadMapStart())
  577. if containerLen == 0 {
  578. d.mapEnd()
  579. return
  580. }
  581. hasLen := containerLen >= 0
  582. var name2 []byte
  583. if mf != nil {
  584. var namearr2 [16]byte
  585. name2 = namearr2[:0]
  586. }
  587. var rvkencname []byte
  588. for j := 0; d.containerNext(j, containerLen, hasLen); j++ {
  589. d.mapElemKey()
  590. if ti.keyType == valueTypeString {
  591. rvkencname = d.d.DecodeStringAsBytes()
  592. } else {
  593. rvkencname = decStructFieldKeyNotString(d.d, ti.keyType, &d.b)
  594. }
  595. d.mapElemValue()
  596. if si := ti.siForEncName(rvkencname); si != nil {
  597. d.decodeValue(si.path.fieldAlloc(rv), nil)
  598. } else if mf != nil {
  599. // store rvkencname in new []byte, as it previously shares Decoder.b, which is used in decode
  600. name2 = append(name2[:0], rvkencname...)
  601. var f interface{}
  602. d.decode(&f)
  603. if !mf.CodecMissingField(name2, f) && d.h.ErrorIfNoField {
  604. d.errorf("no matching struct field when decoding stream map with key: %s ", stringView(name2))
  605. }
  606. } else {
  607. d.structFieldNotFound(-1, stringView(rvkencname))
  608. }
  609. }
  610. d.mapEnd()
  611. } else if ctyp == valueTypeArray {
  612. containerLen := d.arrayStart(d.d.ReadArrayStart())
  613. if containerLen == 0 {
  614. d.arrayEnd()
  615. return
  616. }
  617. // Not much gain from doing it two ways for array.
  618. // Arrays are not used as much for structs.
  619. hasLen := containerLen >= 0
  620. var checkbreak bool
  621. tisfi := ti.sfi.source()
  622. for j, si := range tisfi {
  623. if hasLen {
  624. if j == containerLen {
  625. break
  626. }
  627. } else if d.checkBreak() {
  628. checkbreak = true
  629. break
  630. }
  631. d.arrayElem()
  632. d.decodeValue(si.path.fieldAlloc(rv), nil)
  633. }
  634. var proceed bool
  635. if hasLen {
  636. proceed = containerLen > len(tisfi)
  637. } else {
  638. proceed = !checkbreak
  639. }
  640. // if (hasLen && containerLen > len(tisfi)) || (!hasLen && !checkbreak) {
  641. if proceed {
  642. // read remaining values and throw away
  643. for j := len(tisfi); ; j++ {
  644. if !d.containerNext(j, containerLen, hasLen) {
  645. break
  646. }
  647. d.arrayElem()
  648. d.structFieldNotFound(j, "")
  649. }
  650. }
  651. d.arrayEnd()
  652. } else {
  653. d.onerror(errNeedMapOrArrayDecodeToStruct)
  654. }
  655. }
  656. func (d *Decoder) kSlice(f *codecFnInfo, rv reflect.Value) {
  657. // A slice can be set from a map or array in stream.
  658. // This way, the order can be kept (as order is lost with map).
  659. // Note: rv is a slice type here - guaranteed
  660. ti := f.ti
  661. rvCanset := rv.CanSet()
  662. ctyp := d.d.ContainerType()
  663. if ctyp == valueTypeBytes || ctyp == valueTypeString {
  664. // you can only decode bytes or string in the stream into a slice or array of bytes
  665. if !(ti.rtid == uint8SliceTypId || ti.elemkind == uint8(reflect.Uint8)) {
  666. d.errorf("bytes/string in stream must decode into slice/array of bytes, not %v", ti.rt)
  667. }
  668. rvbs := rvGetBytes(rv)
  669. if !rvCanset {
  670. // not addressable byte slice, so do not decode into it past the length
  671. rvbs = rvbs[:len(rvbs):len(rvbs)]
  672. }
  673. bs2 := d.decodeBytesInto(rvbs)
  674. // if !(len(bs2) == len(rvbs) && byteSliceSameData(rvbs, bs2)) {
  675. if !(len(bs2) > 0 && len(bs2) == len(rvbs) && &bs2[0] == &rvbs[0]) {
  676. if rvCanset {
  677. rvSetBytes(rv, bs2)
  678. } else if len(rvbs) > 0 && len(bs2) > 0 {
  679. copy(rvbs, bs2)
  680. }
  681. }
  682. return
  683. }
  684. slh, containerLenS := d.decSliceHelperStart() // only expects valueType(Array|Map) - never Nil
  685. // an array can never return a nil slice. so no need to check f.array here.
  686. if containerLenS == 0 {
  687. if rvCanset {
  688. if rvIsNil(rv) {
  689. rvSetDirect(rv, rvSliceZeroCap(ti.rt))
  690. } else {
  691. rvSetSliceLen(rv, 0)
  692. }
  693. }
  694. slh.End()
  695. return
  696. }
  697. rtelem0Mut := !scalarBitset.isset(ti.elemkind)
  698. rtelem := ti.elem
  699. for k := reflect.Kind(ti.elemkind); k == reflect.Ptr; k = rtelem.Kind() {
  700. rtelem = rtelem.Elem()
  701. }
  702. var fn *codecFn
  703. var rvChanged bool
  704. var rv0 = rv
  705. var rv9 reflect.Value
  706. rvlen := rvLenSlice(rv)
  707. rvcap := rvCapSlice(rv)
  708. hasLen := containerLenS > 0
  709. if hasLen {
  710. if containerLenS > rvcap {
  711. oldRvlenGtZero := rvlen > 0
  712. rvlen1 := decInferLen(containerLenS, d.h.MaxInitLen, int(ti.elemsize))
  713. if rvlen1 == rvlen {
  714. } else if rvlen1 <= rvcap {
  715. if rvCanset {
  716. rvlen = rvlen1
  717. rvSetSliceLen(rv, rvlen)
  718. }
  719. } else if rvCanset { // rvlen1 > rvcap
  720. rvlen = rvlen1
  721. rv, rvCanset = rvMakeSlice(rv, f.ti, rvlen, rvlen)
  722. rvcap = rvlen
  723. rvChanged = !rvCanset
  724. } else { // rvlen1 > rvcap && !canSet
  725. d.errorf("cannot decode into non-settable slice")
  726. }
  727. if rvChanged && oldRvlenGtZero && rtelem0Mut {
  728. rvCopySlice(rv, rv0, rtelem) // only copy up to length NOT cap i.e. rv0.Slice(0, rvcap)
  729. }
  730. } else if containerLenS != rvlen {
  731. if rvCanset {
  732. rvlen = containerLenS
  733. rvSetSliceLen(rv, rvlen)
  734. }
  735. }
  736. }
  737. // consider creating new element once, and just decoding into it.
  738. var elemReset = d.h.SliceElementReset
  739. var j int
  740. for ; d.containerNext(j, containerLenS, hasLen); j++ {
  741. if j == 0 {
  742. if rvIsNil(rv) { // means hasLen = false
  743. if rvCanset {
  744. rvlen = decInferLen(containerLenS, d.h.MaxInitLen, int(ti.elemsize))
  745. rv, rvCanset = rvMakeSlice(rv, f.ti, rvlen, rvlen)
  746. rvcap = rvlen
  747. rvChanged = !rvCanset
  748. } else {
  749. d.errorf("cannot decode into non-settable slice")
  750. }
  751. }
  752. if fn == nil {
  753. fn = d.h.fn(rtelem)
  754. }
  755. }
  756. // if indefinite, etc, then expand the slice if necessary
  757. if j >= rvlen {
  758. slh.ElemContainerState(j)
  759. // expand the slice up to the cap.
  760. // Note that we did, so we have to reset it later.
  761. if rvlen < rvcap {
  762. rvlen = rvcap
  763. if rvCanset {
  764. rvSetSliceLen(rv, rvlen)
  765. } else if rvChanged {
  766. rv = rvSlice(rv, rvlen)
  767. } else {
  768. d.onerror(errExpandSliceCannotChange)
  769. }
  770. } else {
  771. if !(rvCanset || rvChanged) {
  772. d.onerror(errExpandSliceCannotChange)
  773. }
  774. rv, rvcap, rvCanset = rvGrowSlice(rv, f.ti, rvcap, 1)
  775. rvlen = rvcap
  776. rvChanged = !rvCanset
  777. }
  778. } else {
  779. slh.ElemContainerState(j)
  780. }
  781. rv9 = rvSliceIndex(rv, j, f.ti)
  782. if elemReset {
  783. rvSetZero(rv9)
  784. }
  785. d.decodeValue(rv9, fn)
  786. }
  787. if j < rvlen {
  788. if rvCanset {
  789. rvSetSliceLen(rv, j)
  790. } else if rvChanged {
  791. rv = rvSlice(rv, j)
  792. }
  793. // rvlen = j
  794. } else if j == 0 && rvIsNil(rv) {
  795. if rvCanset {
  796. rv = rvSliceZeroCap(ti.rt)
  797. rvCanset = false
  798. rvChanged = true
  799. }
  800. }
  801. slh.End()
  802. if rvChanged { // infers rvCanset=true, so it can be reset
  803. rvSetDirect(rv0, rv)
  804. }
  805. }
  806. func (d *Decoder) kArray(f *codecFnInfo, rv reflect.Value) {
  807. // An array can be set from a map or array in stream.
  808. ctyp := d.d.ContainerType()
  809. if handleBytesWithinKArray && (ctyp == valueTypeBytes || ctyp == valueTypeString) {
  810. // you can only decode bytes or string in the stream into a slice or array of bytes
  811. if f.ti.elemkind != uint8(reflect.Uint8) {
  812. d.errorf("bytes/string in stream can decode into array of bytes, but not %v", f.ti.rt)
  813. }
  814. rvbs := rvGetArrayBytes(rv, nil)
  815. bs2 := d.decodeBytesInto(rvbs)
  816. if !byteSliceSameData(rvbs, bs2) && len(rvbs) > 0 && len(bs2) > 0 {
  817. copy(rvbs, bs2)
  818. }
  819. return
  820. }
  821. slh, containerLenS := d.decSliceHelperStart() // only expects valueType(Array|Map) - never Nil
  822. // an array can never return a nil slice. so no need to check f.array here.
  823. if containerLenS == 0 {
  824. slh.End()
  825. return
  826. }
  827. rtelem := f.ti.elem
  828. for k := reflect.Kind(f.ti.elemkind); k == reflect.Ptr; k = rtelem.Kind() {
  829. rtelem = rtelem.Elem()
  830. }
  831. var fn *codecFn
  832. var rv9 reflect.Value
  833. rvlen := rv.Len() // same as cap
  834. hasLen := containerLenS > 0
  835. if hasLen && containerLenS > rvlen {
  836. d.errorf("cannot decode into array with length: %v, less than container length: %v", rvlen, containerLenS)
  837. }
  838. // consider creating new element once, and just decoding into it.
  839. var elemReset = d.h.SliceElementReset
  840. for j := 0; d.containerNext(j, containerLenS, hasLen); j++ {
  841. // note that you cannot expand the array if indefinite and we go past array length
  842. if j >= rvlen {
  843. slh.arrayCannotExpand(hasLen, rvlen, j, containerLenS)
  844. return
  845. }
  846. slh.ElemContainerState(j)
  847. rv9 = rvArrayIndex(rv, j, f.ti)
  848. if elemReset {
  849. rvSetZero(rv9)
  850. }
  851. if fn == nil {
  852. fn = d.h.fn(rtelem)
  853. }
  854. d.decodeValue(rv9, fn)
  855. }
  856. slh.End()
  857. }
  858. func (d *Decoder) kChan(f *codecFnInfo, rv reflect.Value) {
  859. // A slice can be set from a map or array in stream.
  860. // This way, the order can be kept (as order is lost with map).
  861. ti := f.ti
  862. if ti.chandir&uint8(reflect.SendDir) == 0 {
  863. d.errorf("receive-only channel cannot be decoded")
  864. }
  865. ctyp := d.d.ContainerType()
  866. if ctyp == valueTypeBytes || ctyp == valueTypeString {
  867. // you can only decode bytes or string in the stream into a slice or array of bytes
  868. if !(ti.rtid == uint8SliceTypId || ti.elemkind == uint8(reflect.Uint8)) {
  869. d.errorf("bytes/string in stream must decode into slice/array of bytes, not %v", ti.rt)
  870. }
  871. bs2 := d.d.DecodeBytes(nil)
  872. irv := rv2i(rv)
  873. ch, ok := irv.(chan<- byte)
  874. if !ok {
  875. ch = irv.(chan byte)
  876. }
  877. for _, b := range bs2 {
  878. ch <- b
  879. }
  880. return
  881. }
  882. var rvCanset = rv.CanSet()
  883. // only expects valueType(Array|Map - nil handled above)
  884. slh, containerLenS := d.decSliceHelperStart()
  885. // an array can never return a nil slice. so no need to check f.array here.
  886. if containerLenS == 0 {
  887. if rvCanset && rvIsNil(rv) {
  888. rvSetDirect(rv, reflect.MakeChan(ti.rt, 0))
  889. }
  890. slh.End()
  891. return
  892. }
  893. rtelem := ti.elem
  894. useTransient := decUseTransient && ti.elemkind != byte(reflect.Ptr) && ti.tielem.flagCanTransient
  895. for k := reflect.Kind(ti.elemkind); k == reflect.Ptr; k = rtelem.Kind() {
  896. rtelem = rtelem.Elem()
  897. }
  898. var fn *codecFn
  899. var rvChanged bool
  900. var rv0 = rv
  901. var rv9 reflect.Value
  902. var rvlen int // = rv.Len()
  903. hasLen := containerLenS > 0
  904. for j := 0; d.containerNext(j, containerLenS, hasLen); j++ {
  905. if j == 0 {
  906. if rvIsNil(rv) {
  907. if hasLen {
  908. rvlen = decInferLen(containerLenS, d.h.MaxInitLen, int(ti.elemsize))
  909. } else {
  910. rvlen = decDefChanCap
  911. }
  912. if rvCanset {
  913. rv = reflect.MakeChan(ti.rt, rvlen)
  914. rvChanged = true
  915. } else {
  916. d.errorf("cannot decode into non-settable chan")
  917. }
  918. }
  919. if fn == nil {
  920. fn = d.h.fn(rtelem)
  921. }
  922. }
  923. slh.ElemContainerState(j)
  924. if rv9.IsValid() {
  925. rvSetZero(rv9)
  926. } else if decUseTransient && useTransient {
  927. rv9 = d.perType.TransientAddrK(ti.elem, reflect.Kind(ti.elemkind))
  928. } else {
  929. rv9 = rvZeroAddrK(ti.elem, reflect.Kind(ti.elemkind))
  930. }
  931. if !d.d.TryNil() {
  932. d.decodeValueNoCheckNil(rv9, fn)
  933. }
  934. rv.Send(rv9)
  935. }
  936. slh.End()
  937. if rvChanged { // infers rvCanset=true, so it can be reset
  938. rvSetDirect(rv0, rv)
  939. }
  940. }
  941. func (d *Decoder) kMap(f *codecFnInfo, rv reflect.Value) {
  942. containerLen := d.mapStart(d.d.ReadMapStart())
  943. ti := f.ti
  944. if rvIsNil(rv) {
  945. rvlen := decInferLen(containerLen, d.h.MaxInitLen, int(ti.keysize+ti.elemsize))
  946. rvSetDirect(rv, makeMapReflect(ti.rt, rvlen))
  947. }
  948. if containerLen == 0 {
  949. d.mapEnd()
  950. return
  951. }
  952. ktype, vtype := ti.key, ti.elem
  953. ktypeId := rt2id(ktype)
  954. vtypeKind := reflect.Kind(ti.elemkind)
  955. ktypeKind := reflect.Kind(ti.keykind)
  956. kfast := mapKeyFastKindFor(ktypeKind)
  957. visindirect := mapStoresElemIndirect(uintptr(ti.elemsize))
  958. visref := refBitset.isset(ti.elemkind)
  959. vtypePtr := vtypeKind == reflect.Ptr
  960. ktypePtr := ktypeKind == reflect.Ptr
  961. vTransient := decUseTransient && !vtypePtr && ti.tielem.flagCanTransient
  962. kTransient := decUseTransient && !ktypePtr && ti.tikey.flagCanTransient
  963. var vtypeElem reflect.Type
  964. var keyFn, valFn *codecFn
  965. var ktypeLo, vtypeLo = ktype, vtype
  966. if ktypeKind == reflect.Ptr {
  967. for ktypeLo = ktype.Elem(); ktypeLo.Kind() == reflect.Ptr; ktypeLo = ktypeLo.Elem() {
  968. }
  969. }
  970. if vtypePtr {
  971. vtypeElem = vtype.Elem()
  972. for vtypeLo = vtypeElem; vtypeLo.Kind() == reflect.Ptr; vtypeLo = vtypeLo.Elem() {
  973. }
  974. }
  975. rvkMut := !scalarBitset.isset(ti.keykind) // if ktype is immutable, then re-use the same rvk.
  976. rvvMut := !scalarBitset.isset(ti.elemkind)
  977. rvvCanNil := isnilBitset.isset(ti.elemkind)
  978. // rvk: key
  979. // rvkn: if non-mutable, on each iteration of loop, set rvk to this
  980. // rvv: value
  981. // rvvn: if non-mutable, on each iteration of loop, set rvv to this
  982. // if mutable, may be used as a temporary value for local-scoped operations
  983. // rvva: if mutable, used as transient value for use for key lookup
  984. // rvvz: zero value of map value type, used to do a map set when nil is found in stream
  985. var rvk, rvkn, rvv, rvvn, rvva, rvvz reflect.Value
  986. // we do a doMapGet if kind is mutable, and InterfaceReset=true if interface
  987. var doMapGet, doMapSet bool
  988. if !d.h.MapValueReset {
  989. if rvvMut && (vtypeKind != reflect.Interface || !d.h.InterfaceReset) {
  990. doMapGet = true
  991. rvva = mapAddrLoopvarRV(vtype, vtypeKind)
  992. }
  993. }
  994. ktypeIsString := ktypeId == stringTypId
  995. ktypeIsIntf := ktypeId == intfTypId
  996. hasLen := containerLen > 0
  997. // kstrbs is used locally for the key bytes, so we can reduce allocation.
  998. // When we read keys, we copy to this local bytes array, and use a stringView for lookup.
  999. // We only convert it into a true string if we have to do a set on the map.
  1000. // Since kstr2bs will usually escape to the heap, declaring a [64]byte array may be wasteful.
  1001. // It is only valuable if we are sure that it is declared on the stack.
  1002. // var kstrarr [64]byte // most keys are less than 32 bytes, and even more less than 64
  1003. // var kstrbs = kstrarr[:0]
  1004. var kstrbs []byte
  1005. var kstr2bs []byte
  1006. var s string
  1007. var callFnRvk bool
  1008. fnRvk2 := func() (s string) {
  1009. callFnRvk = false
  1010. if len(kstr2bs) < 2 {
  1011. return string(kstr2bs)
  1012. }
  1013. return d.mapKeyString(&callFnRvk, &kstrbs, &kstr2bs)
  1014. }
  1015. // Use a possibly transient (map) value (and key), to reduce allocation
  1016. for j := 0; d.containerNext(j, containerLen, hasLen); j++ {
  1017. callFnRvk = false
  1018. if j == 0 {
  1019. // if vtypekind is a scalar and thus value will be decoded using TransientAddrK,
  1020. // then it is ok to use TransientAddr2K for the map key.
  1021. if decUseTransient && vTransient && kTransient {
  1022. rvk = d.perType.TransientAddr2K(ktype, ktypeKind)
  1023. } else {
  1024. rvk = rvZeroAddrK(ktype, ktypeKind)
  1025. }
  1026. if !rvkMut {
  1027. rvkn = rvk
  1028. }
  1029. if !rvvMut {
  1030. if decUseTransient && vTransient {
  1031. rvvn = d.perType.TransientAddrK(vtype, vtypeKind)
  1032. } else {
  1033. rvvn = rvZeroAddrK(vtype, vtypeKind)
  1034. }
  1035. }
  1036. if !ktypeIsString && keyFn == nil {
  1037. keyFn = d.h.fn(ktypeLo)
  1038. }
  1039. if valFn == nil {
  1040. valFn = d.h.fn(vtypeLo)
  1041. }
  1042. } else if rvkMut {
  1043. rvSetZero(rvk)
  1044. } else {
  1045. rvk = rvkn
  1046. }
  1047. d.mapElemKey()
  1048. if ktypeIsString {
  1049. kstr2bs = d.d.DecodeStringAsBytes()
  1050. rvSetString(rvk, fnRvk2())
  1051. } else {
  1052. d.decByteState = decByteStateNone
  1053. d.decodeValue(rvk, keyFn)
  1054. // special case if interface wrapping a byte slice
  1055. if ktypeIsIntf {
  1056. if rvk2 := rvk.Elem(); rvk2.IsValid() && rvType(rvk2) == uint8SliceTyp {
  1057. kstr2bs = rvGetBytes(rvk2)
  1058. rvSetIntf(rvk, rv4istr(fnRvk2()))
  1059. }
  1060. // NOTE: consider failing early if map/slice/func
  1061. }
  1062. }
  1063. d.mapElemValue()
  1064. if d.d.TryNil() {
  1065. // since a map, we have to set zero value if needed
  1066. if !rvvz.IsValid() {
  1067. rvvz = rvZeroK(vtype, vtypeKind)
  1068. }
  1069. if callFnRvk {
  1070. s = d.string(kstr2bs)
  1071. if ktypeIsString {
  1072. rvSetString(rvk, s)
  1073. } else { // ktypeIsIntf
  1074. rvSetIntf(rvk, rv4istr(s))
  1075. }
  1076. }
  1077. mapSet(rv, rvk, rvvz, kfast, visindirect, visref)
  1078. continue
  1079. }
  1080. // there is non-nil content in the stream to decode ...
  1081. // consequently, it's ok to just directly create new value to the pointer (if vtypePtr)
  1082. // set doMapSet to false iff u do a get, and the return value is a non-nil pointer
  1083. doMapSet = true
  1084. if !rvvMut {
  1085. rvv = rvvn
  1086. } else if !doMapGet {
  1087. goto NEW_RVV
  1088. } else {
  1089. rvv = mapGet(rv, rvk, rvva, kfast, visindirect, visref)
  1090. if !rvv.IsValid() || (rvvCanNil && rvIsNil(rvv)) {
  1091. goto NEW_RVV
  1092. }
  1093. switch vtypeKind {
  1094. case reflect.Ptr, reflect.Map: // ok to decode directly into map
  1095. doMapSet = false
  1096. case reflect.Interface:
  1097. // if an interface{}, just decode into it iff a non-nil ptr/map, else allocate afresh
  1098. rvvn = rvv.Elem()
  1099. if k := rvvn.Kind(); (k == reflect.Ptr || k == reflect.Map) && !rvIsNil(rvvn) {
  1100. d.decodeValueNoCheckNil(rvvn, nil) // valFn is incorrect here
  1101. continue
  1102. }
  1103. // make addressable (so we can set the interface)
  1104. rvvn = rvZeroAddrK(vtype, vtypeKind)
  1105. rvSetIntf(rvvn, rvv)
  1106. rvv = rvvn
  1107. default:
  1108. // make addressable (so you can set the slice/array elements, etc)
  1109. if decUseTransient && vTransient {
  1110. rvvn = d.perType.TransientAddrK(vtype, vtypeKind)
  1111. } else {
  1112. rvvn = rvZeroAddrK(vtype, vtypeKind)
  1113. }
  1114. rvSetDirect(rvvn, rvv)
  1115. rvv = rvvn
  1116. }
  1117. }
  1118. goto DECODE_VALUE_NO_CHECK_NIL
  1119. NEW_RVV:
  1120. if vtypePtr {
  1121. rvv = reflect.New(vtypeElem) // non-nil in stream, so allocate value
  1122. } else if decUseTransient && vTransient {
  1123. rvv = d.perType.TransientAddrK(vtype, vtypeKind)
  1124. } else {
  1125. rvv = rvZeroAddrK(vtype, vtypeKind)
  1126. }
  1127. DECODE_VALUE_NO_CHECK_NIL:
  1128. d.decodeValueNoCheckNil(rvv, valFn)
  1129. if doMapSet {
  1130. if callFnRvk {
  1131. s = d.string(kstr2bs)
  1132. if ktypeIsString {
  1133. rvSetString(rvk, s)
  1134. } else { // ktypeIsIntf
  1135. rvSetIntf(rvk, rv4istr(s))
  1136. }
  1137. }
  1138. mapSet(rv, rvk, rvv, kfast, visindirect, visref)
  1139. }
  1140. }
  1141. d.mapEnd()
  1142. }
  1143. // Decoder reads and decodes an object from an input stream in a supported format.
  1144. //
  1145. // Decoder is NOT safe for concurrent use i.e. a Decoder cannot be used
  1146. // concurrently in multiple goroutines.
  1147. //
  1148. // However, as Decoder could be allocation heavy to initialize, a Reset method is provided
  1149. // so its state can be reused to decode new input streams repeatedly.
  1150. // This is the idiomatic way to use.
  1151. type Decoder struct {
  1152. panicHdl
  1153. d decDriver
  1154. // cache the mapTypeId and sliceTypeId for faster comparisons
  1155. mtid uintptr
  1156. stid uintptr
  1157. h *BasicHandle
  1158. blist bytesFreelist
  1159. // ---- cpu cache line boundary?
  1160. decRd
  1161. // ---- cpu cache line boundary?
  1162. n fauxUnion
  1163. hh Handle
  1164. err error
  1165. perType decPerType
  1166. // used for interning strings
  1167. is internerMap
  1168. // ---- cpu cache line boundary?
  1169. // ---- writable fields during execution --- *try* to keep in sep cache line
  1170. maxdepth int16
  1171. depth int16
  1172. // Extensions can call Decode() within a current Decode() call.
  1173. // We need to know when the top level Decode() call returns,
  1174. // so we can decide whether to Release() or not.
  1175. calls uint16 // what depth in mustDecode are we in now.
  1176. c containerState
  1177. decByteState
  1178. // b is an always-available scratch buffer used by Decoder and decDrivers.
  1179. // By being always-available, it can be used for one-off things without
  1180. // having to get from freelist, use, and return back to freelist.
  1181. b [decScratchByteArrayLen]byte
  1182. }
  1183. // NewDecoder returns a Decoder for decoding a stream of bytes from an io.Reader.
  1184. //
  1185. // For efficiency, Users are encouraged to configure ReaderBufferSize on the handle
  1186. // OR pass in a memory buffered reader (eg bufio.Reader, bytes.Buffer).
  1187. func NewDecoder(r io.Reader, h Handle) *Decoder {
  1188. d := h.newDecDriver().decoder()
  1189. if r != nil {
  1190. d.Reset(r)
  1191. }
  1192. return d
  1193. }
  1194. // NewDecoderBytes returns a Decoder which efficiently decodes directly
  1195. // from a byte slice with zero copying.
  1196. func NewDecoderBytes(in []byte, h Handle) *Decoder {
  1197. d := h.newDecDriver().decoder()
  1198. if in != nil {
  1199. d.ResetBytes(in)
  1200. }
  1201. return d
  1202. }
  1203. // NewDecoderString returns a Decoder which efficiently decodes directly
  1204. // from a string with zero copying.
  1205. //
  1206. // It is a convenience function that calls NewDecoderBytes with a
  1207. // []byte view into the string.
  1208. //
  1209. // This can be an efficient zero-copy if using default mode i.e. without codec.safe tag.
  1210. func NewDecoderString(s string, h Handle) *Decoder {
  1211. return NewDecoderBytes(bytesView(s), h)
  1212. }
  1213. func (d *Decoder) r() *decRd {
  1214. return &d.decRd
  1215. }
  1216. func (d *Decoder) init(h Handle) {
  1217. initHandle(h)
  1218. d.bytes = true
  1219. d.err = errDecoderNotInitialized
  1220. d.h = h.getBasicHandle()
  1221. d.hh = h
  1222. d.be = h.isBinary()
  1223. if d.h.InternString && d.is == nil {
  1224. d.is.init()
  1225. }
  1226. // NOTE: do not initialize d.n here. It is lazily initialized in d.naked()
  1227. }
  1228. func (d *Decoder) resetCommon() {
  1229. d.d.reset()
  1230. d.err = nil
  1231. d.c = 0
  1232. d.decByteState = decByteStateNone
  1233. d.depth = 0
  1234. d.calls = 0
  1235. // reset all things which were cached from the Handle, but could change
  1236. d.maxdepth = decDefMaxDepth
  1237. if d.h.MaxDepth > 0 {
  1238. d.maxdepth = d.h.MaxDepth
  1239. }
  1240. d.mtid = 0
  1241. d.stid = 0
  1242. d.mtr = false
  1243. d.str = false
  1244. if d.h.MapType != nil {
  1245. d.mtid = rt2id(d.h.MapType)
  1246. d.mtr = fastpathAvIndex(d.mtid) != -1
  1247. }
  1248. if d.h.SliceType != nil {
  1249. d.stid = rt2id(d.h.SliceType)
  1250. d.str = fastpathAvIndex(d.stid) != -1
  1251. }
  1252. }
  1253. // Reset the Decoder with a new Reader to decode from,
  1254. // clearing all state from last run(s).
  1255. func (d *Decoder) Reset(r io.Reader) {
  1256. if r == nil {
  1257. r = &eofReader
  1258. }
  1259. d.bytes = false
  1260. if d.h.ReaderBufferSize > 0 {
  1261. if d.bi == nil {
  1262. d.bi = new(bufioDecReader)
  1263. }
  1264. d.bi.reset(r, d.h.ReaderBufferSize, &d.blist)
  1265. d.bufio = true
  1266. d.decReader = d.bi
  1267. } else {
  1268. if d.ri == nil {
  1269. d.ri = new(ioDecReader)
  1270. }
  1271. d.ri.reset(r, &d.blist)
  1272. d.bufio = false
  1273. d.decReader = d.ri
  1274. }
  1275. d.resetCommon()
  1276. }
  1277. // ResetBytes resets the Decoder with a new []byte to decode from,
  1278. // clearing all state from last run(s).
  1279. func (d *Decoder) ResetBytes(in []byte) {
  1280. if in == nil {
  1281. in = []byte{}
  1282. }
  1283. d.bufio = false
  1284. d.bytes = true
  1285. d.decReader = &d.rb
  1286. d.rb.reset(in)
  1287. d.resetCommon()
  1288. }
  1289. // ResetString resets the Decoder with a new string to decode from,
  1290. // clearing all state from last run(s).
  1291. //
  1292. // It is a convenience function that calls ResetBytes with a
  1293. // []byte view into the string.
  1294. //
  1295. // This can be an efficient zero-copy if using default mode i.e. without codec.safe tag.
  1296. func (d *Decoder) ResetString(s string) {
  1297. d.ResetBytes(bytesView(s))
  1298. }
  1299. func (d *Decoder) naked() *fauxUnion {
  1300. return &d.n
  1301. }
  1302. // Decode decodes the stream from reader and stores the result in the
  1303. // value pointed to by v. v cannot be a nil pointer. v can also be
  1304. // a reflect.Value of a pointer.
  1305. //
  1306. // Note that a pointer to a nil interface is not a nil pointer.
  1307. // If you do not know what type of stream it is, pass in a pointer to a nil interface.
  1308. // We will decode and store a value in that nil interface.
  1309. //
  1310. // Sample usages:
  1311. // // Decoding into a non-nil typed value
  1312. // var f float32
  1313. // err = codec.NewDecoder(r, handle).Decode(&f)
  1314. //
  1315. // // Decoding into nil interface
  1316. // var v interface{}
  1317. // dec := codec.NewDecoder(r, handle)
  1318. // err = dec.Decode(&v)
  1319. //
  1320. // When decoding into a nil interface{}, we will decode into an appropriate value based
  1321. // on the contents of the stream:
  1322. // - Numbers are decoded as float64, int64 or uint64.
  1323. // - Other values are decoded appropriately depending on the type:
  1324. // bool, string, []byte, time.Time, etc
  1325. // - Extensions are decoded as RawExt (if no ext function registered for the tag)
  1326. // Configurations exist on the Handle to override defaults
  1327. // (e.g. for MapType, SliceType and how to decode raw bytes).
  1328. //
  1329. // When decoding into a non-nil interface{} value, the mode of encoding is based on the
  1330. // type of the value. When a value is seen:
  1331. // - If an extension is registered for it, call that extension function
  1332. // - If it implements BinaryUnmarshaler, call its UnmarshalBinary(data []byte) error
  1333. // - Else decode it based on its reflect.Kind
  1334. //
  1335. // There are some special rules when decoding into containers (slice/array/map/struct).
  1336. // Decode will typically use the stream contents to UPDATE the container i.e. the values
  1337. // in these containers will not be zero'ed before decoding.
  1338. // - A map can be decoded from a stream map, by updating matching keys.
  1339. // - A slice can be decoded from a stream array,
  1340. // by updating the first n elements, where n is length of the stream.
  1341. // - A slice can be decoded from a stream map, by decoding as if
  1342. // it contains a sequence of key-value pairs.
  1343. // - A struct can be decoded from a stream map, by updating matching fields.
  1344. // - A struct can be decoded from a stream array,
  1345. // by updating fields as they occur in the struct (by index).
  1346. //
  1347. // This in-place update maintains consistency in the decoding philosophy (i.e. we ALWAYS update
  1348. // in place by default). However, the consequence of this is that values in slices or maps
  1349. // which are not zero'ed before hand, will have part of the prior values in place after decode
  1350. // if the stream doesn't contain an update for those parts.
  1351. //
  1352. // This in-place update can be disabled by configuring the MapValueReset and SliceElementReset
  1353. // decode options available on every handle.
  1354. //
  1355. // Furthermore, when decoding a stream map or array with length of 0 into a nil map or slice,
  1356. // we reset the destination map or slice to a zero-length value.
  1357. //
  1358. // However, when decoding a stream nil, we reset the destination container
  1359. // to its "zero" value (e.g. nil for slice/map, etc).
  1360. //
  1361. // Note: we allow nil values in the stream anywhere except for map keys.
  1362. // A nil value in the encoded stream where a map key is expected is treated as an error.
  1363. func (d *Decoder) Decode(v interface{}) (err error) {
  1364. // tried to use closure, as runtime optimizes defer with no params.
  1365. // This seemed to be causing weird issues (like circular reference found, unexpected panic, etc).
  1366. // Also, see https://github.com/golang/go/issues/14939#issuecomment-417836139
  1367. if !debugging {
  1368. defer func() {
  1369. if x := recover(); x != nil {
  1370. panicValToErr(d, x, &d.err)
  1371. err = d.err
  1372. }
  1373. }()
  1374. }
  1375. d.MustDecode(v)
  1376. return
  1377. }
  1378. // MustDecode is like Decode, but panics if unable to Decode.
  1379. //
  1380. // Note: This provides insight to the code location that triggered the error.
  1381. func (d *Decoder) MustDecode(v interface{}) {
  1382. halt.onerror(d.err)
  1383. if d.hh == nil {
  1384. halt.onerror(errNoFormatHandle)
  1385. }
  1386. // Top-level: v is a pointer and not nil.
  1387. d.calls++
  1388. d.decode(v)
  1389. d.calls--
  1390. }
  1391. // Release releases shared (pooled) resources.
  1392. //
  1393. // It is important to call Release() when done with a Decoder, so those resources
  1394. // are released instantly for use by subsequently created Decoders.
  1395. //
  1396. // By default, Release() is automatically called unless the option ExplicitRelease is set.
  1397. //
  1398. // Deprecated: Release is a no-op as pooled resources are not used with an Decoder.
  1399. // This method is kept for compatibility reasons only.
  1400. func (d *Decoder) Release() {
  1401. }
  1402. func (d *Decoder) swallow() {
  1403. d.d.nextValueBytes(nil)
  1404. }
  1405. func (d *Decoder) swallowErr() (err error) {
  1406. if !debugging {
  1407. defer func() {
  1408. if x := recover(); x != nil {
  1409. panicValToErr(d, x, &err)
  1410. }
  1411. }()
  1412. }
  1413. d.swallow()
  1414. return
  1415. }
  1416. func setZero(iv interface{}) {
  1417. if iv == nil {
  1418. return
  1419. }
  1420. rv, ok := isNil(iv)
  1421. if ok {
  1422. return
  1423. }
  1424. // var canDecode bool
  1425. switch v := iv.(type) {
  1426. case *string:
  1427. *v = ""
  1428. case *bool:
  1429. *v = false
  1430. case *int:
  1431. *v = 0
  1432. case *int8:
  1433. *v = 0
  1434. case *int16:
  1435. *v = 0
  1436. case *int32:
  1437. *v = 0
  1438. case *int64:
  1439. *v = 0
  1440. case *uint:
  1441. *v = 0
  1442. case *uint8:
  1443. *v = 0
  1444. case *uint16:
  1445. *v = 0
  1446. case *uint32:
  1447. *v = 0
  1448. case *uint64:
  1449. *v = 0
  1450. case *float32:
  1451. *v = 0
  1452. case *float64:
  1453. *v = 0
  1454. case *complex64:
  1455. *v = 0
  1456. case *complex128:
  1457. *v = 0
  1458. case *[]byte:
  1459. *v = nil
  1460. case *Raw:
  1461. *v = nil
  1462. case *time.Time:
  1463. *v = time.Time{}
  1464. case reflect.Value:
  1465. decSetNonNilRV2Zero(v)
  1466. default:
  1467. if !fastpathDecodeSetZeroTypeSwitch(iv) {
  1468. decSetNonNilRV2Zero(rv)
  1469. }
  1470. }
  1471. }
  1472. // decSetNonNilRV2Zero will set the non-nil value to its zero value.
  1473. func decSetNonNilRV2Zero(v reflect.Value) {
  1474. // If not decodeable (settable), we do not touch it.
  1475. // We considered empty'ing it if not decodeable e.g.
  1476. // - if chan, drain it
  1477. // - if map, clear it
  1478. // - if slice or array, zero all elements up to len
  1479. //
  1480. // However, we decided instead that we either will set the
  1481. // whole value to the zero value, or leave AS IS.
  1482. k := v.Kind()
  1483. if k == reflect.Interface {
  1484. decSetNonNilRV2Zero4Intf(v)
  1485. } else if k == reflect.Ptr {
  1486. decSetNonNilRV2Zero4Ptr(v)
  1487. } else if v.CanSet() {
  1488. rvSetDirectZero(v)
  1489. }
  1490. }
  1491. func decSetNonNilRV2Zero4Ptr(v reflect.Value) {
  1492. ve := v.Elem()
  1493. if ve.CanSet() {
  1494. rvSetZero(ve) // we can have a pointer to an interface
  1495. } else if v.CanSet() {
  1496. rvSetZero(v)
  1497. }
  1498. }
  1499. func decSetNonNilRV2Zero4Intf(v reflect.Value) {
  1500. ve := v.Elem()
  1501. if ve.CanSet() {
  1502. rvSetDirectZero(ve) // interfaces always have element as a non-interface
  1503. } else if v.CanSet() {
  1504. rvSetZero(v)
  1505. }
  1506. }
  1507. func (d *Decoder) decode(iv interface{}) {
  1508. // a switch with only concrete types can be optimized.
  1509. // consequently, we deal with nil and interfaces outside the switch.
  1510. if iv == nil {
  1511. d.onerror(errCannotDecodeIntoNil)
  1512. }
  1513. switch v := iv.(type) {
  1514. // case nil:
  1515. // case Selfer:
  1516. case reflect.Value:
  1517. if x, _ := isDecodeable(v); !x {
  1518. d.haltAsNotDecodeable(v)
  1519. }
  1520. d.decodeValue(v, nil)
  1521. case *string:
  1522. *v = d.stringZC(d.d.DecodeStringAsBytes())
  1523. case *bool:
  1524. *v = d.d.DecodeBool()
  1525. case *int:
  1526. *v = int(chkOvf.IntV(d.d.DecodeInt64(), intBitsize))
  1527. case *int8:
  1528. *v = int8(chkOvf.IntV(d.d.DecodeInt64(), 8))
  1529. case *int16:
  1530. *v = int16(chkOvf.IntV(d.d.DecodeInt64(), 16))
  1531. case *int32:
  1532. *v = int32(chkOvf.IntV(d.d.DecodeInt64(), 32))
  1533. case *int64:
  1534. *v = d.d.DecodeInt64()
  1535. case *uint:
  1536. *v = uint(chkOvf.UintV(d.d.DecodeUint64(), uintBitsize))
  1537. case *uint8:
  1538. *v = uint8(chkOvf.UintV(d.d.DecodeUint64(), 8))
  1539. case *uint16:
  1540. *v = uint16(chkOvf.UintV(d.d.DecodeUint64(), 16))
  1541. case *uint32:
  1542. *v = uint32(chkOvf.UintV(d.d.DecodeUint64(), 32))
  1543. case *uint64:
  1544. *v = d.d.DecodeUint64()
  1545. case *float32:
  1546. *v = d.decodeFloat32()
  1547. case *float64:
  1548. *v = d.d.DecodeFloat64()
  1549. case *complex64:
  1550. *v = complex(d.decodeFloat32(), 0)
  1551. case *complex128:
  1552. *v = complex(d.d.DecodeFloat64(), 0)
  1553. case *[]byte:
  1554. *v = d.decodeBytesInto(*v)
  1555. case []byte:
  1556. // not addressable byte slice, so do not decode into it past the length
  1557. b := d.decodeBytesInto(v[:len(v):len(v)])
  1558. if !(len(b) > 0 && len(b) == len(v) && &b[0] == &v[0]) { // not same slice
  1559. copy(v, b)
  1560. }
  1561. case *time.Time:
  1562. *v = d.d.DecodeTime()
  1563. case *Raw:
  1564. *v = d.rawBytes()
  1565. case *interface{}:
  1566. d.decodeValue(rv4iptr(v), nil)
  1567. default:
  1568. // we can't check non-predefined types, as they might be a Selfer or extension.
  1569. if skipFastpathTypeSwitchInDirectCall || !fastpathDecodeTypeSwitch(iv, d) {
  1570. v := reflect.ValueOf(iv)
  1571. if x, _ := isDecodeable(v); !x {
  1572. d.haltAsNotDecodeable(v)
  1573. }
  1574. d.decodeValue(v, nil)
  1575. }
  1576. }
  1577. }
  1578. // decodeValue MUST be called by the actual value we want to decode into,
  1579. // not its addr or a reference to it.
  1580. //
  1581. // This way, we know if it is itself a pointer, and can handle nil in
  1582. // the stream effectively.
  1583. //
  1584. // Note that decodeValue will handle nil in the stream early, so that the
  1585. // subsequent calls i.e. kXXX methods, etc do not have to handle it themselves.
  1586. func (d *Decoder) decodeValue(rv reflect.Value, fn *codecFn) {
  1587. if d.d.TryNil() {
  1588. decSetNonNilRV2Zero(rv)
  1589. return
  1590. }
  1591. d.decodeValueNoCheckNil(rv, fn)
  1592. }
  1593. func (d *Decoder) decodeValueNoCheckNil(rv reflect.Value, fn *codecFn) {
  1594. // If stream is not containing a nil value, then we can deref to the base
  1595. // non-pointer value, and decode into that.
  1596. var rvp reflect.Value
  1597. var rvpValid bool
  1598. PTR:
  1599. if rv.Kind() == reflect.Ptr {
  1600. rvpValid = true
  1601. if rvIsNil(rv) {
  1602. rvSetDirect(rv, reflect.New(rvType(rv).Elem()))
  1603. }
  1604. rvp = rv
  1605. rv = rv.Elem()
  1606. goto PTR
  1607. }
  1608. if fn == nil {
  1609. fn = d.h.fn(rvType(rv))
  1610. }
  1611. if fn.i.addrD {
  1612. if rvpValid {
  1613. rv = rvp
  1614. } else if rv.CanAddr() {
  1615. rv = rvAddr(rv, fn.i.ti.ptr)
  1616. } else if fn.i.addrDf {
  1617. d.errorf("cannot decode into a non-pointer value")
  1618. }
  1619. }
  1620. fn.fd(d, &fn.i, rv)
  1621. }
  1622. func (d *Decoder) structFieldNotFound(index int, rvkencname string) {
  1623. // Note: rvkencname is used only if there is an error, to pass into d.errorf.
  1624. // Consequently, it is ok to pass in a stringView
  1625. // Since rvkencname may be a stringView, do NOT pass it to another function.
  1626. if d.h.ErrorIfNoField {
  1627. if index >= 0 {
  1628. d.errorf("no matching struct field found when decoding stream array at index %v", index)
  1629. } else if rvkencname != "" {
  1630. d.errorf("no matching struct field found when decoding stream map with key " + rvkencname)
  1631. }
  1632. }
  1633. d.swallow()
  1634. }
  1635. func (d *Decoder) arrayCannotExpand(sliceLen, streamLen int) {
  1636. if d.h.ErrorIfNoArrayExpand {
  1637. d.errorf("cannot expand array len during decode from %v to %v", sliceLen, streamLen)
  1638. }
  1639. }
  1640. func (d *Decoder) haltAsNotDecodeable(rv reflect.Value) {
  1641. if !rv.IsValid() {
  1642. d.onerror(errCannotDecodeIntoNil)
  1643. }
  1644. // check if an interface can be retrieved, before grabbing an interface
  1645. if !rv.CanInterface() {
  1646. d.errorf("cannot decode into a value without an interface: %v", rv)
  1647. }
  1648. d.errorf("cannot decode into value of kind: %v, %#v", rv.Kind(), rv2i(rv))
  1649. }
  1650. func (d *Decoder) depthIncr() {
  1651. d.depth++
  1652. if d.depth >= d.maxdepth {
  1653. d.onerror(errMaxDepthExceeded)
  1654. }
  1655. }
  1656. func (d *Decoder) depthDecr() {
  1657. d.depth--
  1658. }
  1659. // Possibly get an interned version of a string, iff InternString=true and decoding a map key.
  1660. //
  1661. // This should mostly be used for map keys, where the key type is string.
  1662. // This is because keys of a map/struct are typically reused across many objects.
  1663. func (d *Decoder) string(v []byte) (s string) {
  1664. if d.is == nil || d.c != containerMapKey || len(v) < 2 || len(v) > internMaxStrLen {
  1665. return string(v)
  1666. }
  1667. return d.is.string(v)
  1668. }
  1669. func (d *Decoder) zerocopy() bool {
  1670. return d.bytes && d.h.ZeroCopy
  1671. }
  1672. // decodeBytesInto is a convenience delegate function to decDriver.DecodeBytes.
  1673. // It ensures that `in` is not a nil byte, before calling decDriver.DecodeBytes,
  1674. // as decDriver.DecodeBytes treats a nil as a hint to use its internal scratch buffer.
  1675. func (d *Decoder) decodeBytesInto(in []byte) (v []byte) {
  1676. if in == nil {
  1677. in = []byte{}
  1678. }
  1679. return d.d.DecodeBytes(in)
  1680. }
  1681. func (d *Decoder) rawBytes() (v []byte) {
  1682. // ensure that this is not a view into the bytes
  1683. // i.e. if necessary, make new copy always.
  1684. v = d.d.nextValueBytes([]byte{})
  1685. if d.bytes && !d.h.ZeroCopy {
  1686. v0 := v
  1687. v = make([]byte, len(v))
  1688. copy(v, v0)
  1689. }
  1690. return
  1691. }
  1692. func (d *Decoder) wrapErr(v error, err *error) {
  1693. *err = wrapCodecErr(v, d.hh.Name(), d.NumBytesRead(), false)
  1694. }
  1695. // NumBytesRead returns the number of bytes read
  1696. func (d *Decoder) NumBytesRead() int {
  1697. return int(d.r().numread())
  1698. }
  1699. // decodeFloat32 will delegate to an appropriate DecodeFloat32 implementation (if exists),
  1700. // else if will call DecodeFloat64 and ensure the value doesn't overflow.
  1701. //
  1702. // Note that we return float64 to reduce unnecessary conversions
  1703. func (d *Decoder) decodeFloat32() float32 {
  1704. if d.js {
  1705. return d.jsondriver().DecodeFloat32() // custom implementation for 32-bit
  1706. }
  1707. return float32(chkOvf.Float32V(d.d.DecodeFloat64()))
  1708. }
  1709. // ---- container tracking
  1710. // Note: We update the .c after calling the callback.
  1711. // This way, the callback can know what the last status was.
  1712. // MARKER: do not call mapEnd if mapStart returns containerLenNil.
  1713. func (d *Decoder) containerNext(j, containerLen int, hasLen bool) bool {
  1714. // return (hasLen && j < containerLen) || !(hasLen || slh.d.checkBreak())
  1715. if hasLen {
  1716. return j < containerLen
  1717. }
  1718. return !d.checkBreak()
  1719. }
  1720. func (d *Decoder) mapStart(v int) int {
  1721. if v != containerLenNil {
  1722. d.depthIncr()
  1723. d.c = containerMapStart
  1724. }
  1725. return v
  1726. }
  1727. func (d *Decoder) mapElemKey() {
  1728. if d.js {
  1729. d.jsondriver().ReadMapElemKey()
  1730. }
  1731. d.c = containerMapKey
  1732. }
  1733. func (d *Decoder) mapElemValue() {
  1734. if d.js {
  1735. d.jsondriver().ReadMapElemValue()
  1736. }
  1737. d.c = containerMapValue
  1738. }
  1739. func (d *Decoder) mapEnd() {
  1740. d.d.ReadMapEnd()
  1741. d.depthDecr()
  1742. d.c = 0
  1743. }
  1744. func (d *Decoder) arrayStart(v int) int {
  1745. if v != containerLenNil {
  1746. d.depthIncr()
  1747. d.c = containerArrayStart
  1748. }
  1749. return v
  1750. }
  1751. func (d *Decoder) arrayElem() {
  1752. if d.js {
  1753. d.jsondriver().ReadArrayElem()
  1754. }
  1755. d.c = containerArrayElem
  1756. }
  1757. func (d *Decoder) arrayEnd() {
  1758. d.d.ReadArrayEnd()
  1759. d.depthDecr()
  1760. d.c = 0
  1761. }
  1762. func (d *Decoder) interfaceExtConvertAndDecode(v interface{}, ext InterfaceExt) {
  1763. // var v interface{} = ext.ConvertExt(rv)
  1764. // d.d.decode(&v)
  1765. // ext.UpdateExt(rv, v)
  1766. // assume v is a pointer:
  1767. // - if struct|array, pass as is to ConvertExt
  1768. // - else make it non-addressable and pass to ConvertExt
  1769. // - make return value from ConvertExt addressable
  1770. // - decode into it
  1771. // - return the interface for passing into UpdateExt.
  1772. // - interface should be a pointer if struct|array, else a value
  1773. var s interface{}
  1774. rv := reflect.ValueOf(v)
  1775. rv2 := rv.Elem()
  1776. rvk := rv2.Kind()
  1777. if rvk == reflect.Struct || rvk == reflect.Array {
  1778. s = ext.ConvertExt(v)
  1779. } else {
  1780. s = ext.ConvertExt(rv2i(rv2))
  1781. }
  1782. rv = reflect.ValueOf(s)
  1783. // We cannot use isDecodeable here, as the value converted may be nil,
  1784. // or it may not be nil but is not addressable and thus we cannot extend it, etc.
  1785. // Instead, we just ensure that the value is addressable.
  1786. if !rv.CanAddr() {
  1787. rvk = rv.Kind()
  1788. rv2 = d.oneShotAddrRV(rvType(rv), rvk)
  1789. if rvk == reflect.Interface {
  1790. rvSetIntf(rv2, rv)
  1791. } else {
  1792. rvSetDirect(rv2, rv)
  1793. }
  1794. rv = rv2
  1795. }
  1796. d.decodeValue(rv, nil)
  1797. ext.UpdateExt(v, rv2i(rv))
  1798. }
  1799. func (d *Decoder) sideDecode(v interface{}, basetype reflect.Type, bs []byte) {
  1800. // NewDecoderBytes(bs, d.hh).decodeValue(baseRV(v), d.h.fnNoExt(basetype))
  1801. defer func(rb bytesDecReader, bytes bool,
  1802. c containerState, dbs decByteState, depth int16, r decReader, state interface{}) {
  1803. d.rb = rb
  1804. d.bytes = bytes
  1805. d.c = c
  1806. d.decByteState = dbs
  1807. d.depth = depth
  1808. d.decReader = r
  1809. d.d.restoreState(state)
  1810. }(d.rb, d.bytes, d.c, d.decByteState, d.depth, d.decReader, d.d.captureState())
  1811. // d.rb.reset(in)
  1812. d.rb = bytesDecReader{bs[:len(bs):len(bs)], 0}
  1813. d.bytes = true
  1814. d.decReader = &d.rb
  1815. d.d.resetState()
  1816. d.c = 0
  1817. d.decByteState = decByteStateNone
  1818. d.depth = 0
  1819. // must call using fnNoExt
  1820. d.decodeValue(baseRV(v), d.h.fnNoExt(basetype))
  1821. }
  1822. func (d *Decoder) fauxUnionReadRawBytes(asString bool) {
  1823. if asString || d.h.RawToString {
  1824. d.n.v = valueTypeString
  1825. // fauxUnion is only used within DecodeNaked calls; consequently, we should try to intern.
  1826. d.n.s = d.stringZC(d.d.DecodeBytes(nil))
  1827. } else {
  1828. d.n.v = valueTypeBytes
  1829. d.n.l = d.d.DecodeBytes([]byte{})
  1830. }
  1831. }
  1832. func (d *Decoder) oneShotAddrRV(rvt reflect.Type, rvk reflect.Kind) reflect.Value {
  1833. if decUseTransient &&
  1834. (numBoolStrSliceBitset.isset(byte(rvk)) ||
  1835. ((rvk == reflect.Struct || rvk == reflect.Array) &&
  1836. d.h.getTypeInfo(rt2id(rvt), rvt).flagCanTransient)) {
  1837. return d.perType.TransientAddrK(rvt, rvk)
  1838. }
  1839. return rvZeroAddrK(rvt, rvk)
  1840. }
  1841. // --------------------------------------------------
  1842. // decSliceHelper assists when decoding into a slice, from a map or an array in the stream.
  1843. // A slice can be set from a map or array in stream. This supports the MapBySlice interface.
  1844. //
  1845. // Note: if IsNil, do not call ElemContainerState.
  1846. type decSliceHelper struct {
  1847. d *Decoder
  1848. ct valueType
  1849. Array bool
  1850. IsNil bool
  1851. }
  1852. func (d *Decoder) decSliceHelperStart() (x decSliceHelper, clen int) {
  1853. x.ct = d.d.ContainerType()
  1854. x.d = d
  1855. switch x.ct {
  1856. case valueTypeNil:
  1857. x.IsNil = true
  1858. case valueTypeArray:
  1859. x.Array = true
  1860. clen = d.arrayStart(d.d.ReadArrayStart())
  1861. case valueTypeMap:
  1862. clen = d.mapStart(d.d.ReadMapStart())
  1863. clen += clen
  1864. default:
  1865. d.errorf("only encoded map or array can be decoded into a slice (%d)", x.ct)
  1866. }
  1867. return
  1868. }
  1869. func (x decSliceHelper) End() {
  1870. if x.IsNil {
  1871. } else if x.Array {
  1872. x.d.arrayEnd()
  1873. } else {
  1874. x.d.mapEnd()
  1875. }
  1876. }
  1877. func (x decSliceHelper) ElemContainerState(index int) {
  1878. // Note: if isnil, clen=0, so we never call into ElemContainerState
  1879. if x.Array {
  1880. x.d.arrayElem()
  1881. } else if index&1 == 0 { // index%2 == 0 {
  1882. x.d.mapElemKey()
  1883. } else {
  1884. x.d.mapElemValue()
  1885. }
  1886. }
  1887. func (x decSliceHelper) arrayCannotExpand(hasLen bool, lenv, j, containerLenS int) {
  1888. x.d.arrayCannotExpand(lenv, j+1)
  1889. // drain completely and return
  1890. x.ElemContainerState(j)
  1891. x.d.swallow()
  1892. j++
  1893. for ; x.d.containerNext(j, containerLenS, hasLen); j++ {
  1894. x.ElemContainerState(j)
  1895. x.d.swallow()
  1896. }
  1897. x.End()
  1898. }
  1899. // decNextValueBytesHelper helps with NextValueBytes calls.
  1900. //
  1901. // Typical usage:
  1902. // - each Handle's decDriver will implement a high level nextValueBytes,
  1903. // which will track the current cursor, delegate to a nextValueBytesR
  1904. // method, and then potentially call bytesRdV at the end.
  1905. //
  1906. // See simple.go for typical usage model.
  1907. type decNextValueBytesHelper struct {
  1908. d *Decoder
  1909. }
  1910. func (x decNextValueBytesHelper) append1(v *[]byte, b byte) {
  1911. if *v != nil && !x.d.bytes {
  1912. *v = append(*v, b)
  1913. }
  1914. }
  1915. func (x decNextValueBytesHelper) appendN(v *[]byte, b ...byte) {
  1916. if *v != nil && !x.d.bytes {
  1917. *v = append(*v, b...)
  1918. }
  1919. }
  1920. func (x decNextValueBytesHelper) bytesRdV(v *[]byte, startpos uint) {
  1921. if x.d.bytes {
  1922. *v = x.d.rb.b[startpos:x.d.rb.c]
  1923. }
  1924. }
  1925. // decNegintPosintFloatNumberHelper is used for formats that are binary
  1926. // and have distinct ways of storing positive integers vs negative integers
  1927. // vs floats, which are uniquely identified by the byte descriptor.
  1928. //
  1929. // Currently, these formats are binc, cbor and simple.
  1930. type decNegintPosintFloatNumberHelper struct {
  1931. d *Decoder
  1932. }
  1933. func (x decNegintPosintFloatNumberHelper) uint64(ui uint64, neg, ok bool) uint64 {
  1934. if ok && !neg {
  1935. return ui
  1936. }
  1937. return x.uint64TryFloat(ok)
  1938. }
  1939. func (x decNegintPosintFloatNumberHelper) uint64TryFloat(ok bool) (ui uint64) {
  1940. if ok { // neg = true
  1941. x.d.errorf("assigning negative signed value to unsigned type")
  1942. }
  1943. f, ok := x.d.d.decFloat()
  1944. if ok && f >= 0 && noFrac64(math.Float64bits(f)) {
  1945. ui = uint64(f)
  1946. } else {
  1947. x.d.errorf("invalid number loading uint64, with descriptor: %v", x.d.d.descBd())
  1948. }
  1949. return ui
  1950. }
  1951. func decNegintPosintFloatNumberHelperInt64v(ui uint64, neg, incrIfNeg bool) (i int64) {
  1952. if neg && incrIfNeg {
  1953. ui++
  1954. }
  1955. i = chkOvf.SignedIntV(ui)
  1956. if neg {
  1957. i = -i
  1958. }
  1959. return
  1960. }
  1961. func (x decNegintPosintFloatNumberHelper) int64(ui uint64, neg, ok bool) (i int64) {
  1962. if ok {
  1963. return decNegintPosintFloatNumberHelperInt64v(ui, neg, x.d.cbor)
  1964. }
  1965. // return x.int64TryFloat()
  1966. // }
  1967. // func (x decNegintPosintFloatNumberHelper) int64TryFloat() (i int64) {
  1968. f, ok := x.d.d.decFloat()
  1969. if ok && noFrac64(math.Float64bits(f)) {
  1970. i = int64(f)
  1971. } else {
  1972. x.d.errorf("invalid number loading uint64, with descriptor: %v", x.d.d.descBd())
  1973. }
  1974. return
  1975. }
  1976. func (x decNegintPosintFloatNumberHelper) float64(f float64, ok bool) float64 {
  1977. if ok {
  1978. return f
  1979. }
  1980. return x.float64TryInteger()
  1981. }
  1982. func (x decNegintPosintFloatNumberHelper) float64TryInteger() float64 {
  1983. ui, neg, ok := x.d.d.decInteger()
  1984. if !ok {
  1985. x.d.errorf("invalid descriptor for float: %v", x.d.d.descBd())
  1986. }
  1987. return float64(decNegintPosintFloatNumberHelperInt64v(ui, neg, x.d.cbor))
  1988. }
  1989. // isDecodeable checks if value can be decoded into
  1990. //
  1991. // decode can take any reflect.Value that is a inherently addressable i.e.
  1992. // - non-nil chan (we will SEND to it)
  1993. // - non-nil slice (we will set its elements)
  1994. // - non-nil map (we will put into it)
  1995. // - non-nil pointer (we can "update" it)
  1996. // - func: no
  1997. // - interface: no
  1998. // - array: if canAddr=true
  1999. // - any other value pointer: if canAddr=true
  2000. func isDecodeable(rv reflect.Value) (canDecode bool, reason decNotDecodeableReason) {
  2001. switch rv.Kind() {
  2002. case reflect.Ptr, reflect.Slice, reflect.Chan, reflect.Map:
  2003. canDecode = !rvIsNil(rv)
  2004. reason = decNotDecodeableReasonNilReference
  2005. case reflect.Func, reflect.Interface, reflect.Invalid, reflect.UnsafePointer:
  2006. reason = decNotDecodeableReasonBadKind
  2007. default:
  2008. canDecode = rv.CanAddr()
  2009. reason = decNotDecodeableReasonNonAddrValue
  2010. }
  2011. return
  2012. }
  2013. func decByteSlice(r *decRd, clen, maxInitLen int, bs []byte) (bsOut []byte) {
  2014. if clen == 0 {
  2015. return zeroByteSlice
  2016. }
  2017. if len(bs) == clen {
  2018. bsOut = bs
  2019. r.readb(bsOut)
  2020. } else if cap(bs) >= clen {
  2021. bsOut = bs[:clen]
  2022. r.readb(bsOut)
  2023. } else {
  2024. var len2 int
  2025. for len2 < clen {
  2026. len3 := decInferLen(clen-len2, maxInitLen, 1)
  2027. bs3 := bsOut
  2028. bsOut = make([]byte, len2+len3)
  2029. copy(bsOut, bs3)
  2030. r.readb(bsOut[len2:])
  2031. len2 += len3
  2032. }
  2033. }
  2034. return
  2035. }
  2036. // decInferLen will infer a sensible length, given the following:
  2037. // - clen: length wanted.
  2038. // - maxlen: max length to be returned.
  2039. // if <= 0, it is unset, and we infer it based on the unit size
  2040. // - unit: number of bytes for each element of the collection
  2041. func decInferLen(clen, maxlen, unit int) int {
  2042. // anecdotal testing showed increase in allocation with map length of 16.
  2043. // We saw same typical alloc from 0-8, then a 20% increase at 16.
  2044. // Thus, we set it to 8.
  2045. const (
  2046. minLenIfUnset = 8
  2047. maxMem = 256 * 1024 // 256Kb Memory
  2048. )
  2049. // handle when maxlen is not set i.e. <= 0
  2050. // clen==0: use 0
  2051. // maxlen<=0, clen<0: use default
  2052. // maxlen> 0, clen<0: use default
  2053. // maxlen<=0, clen>0: infer maxlen, and cap on it
  2054. // maxlen> 0, clen>0: cap at maxlen
  2055. if clen == 0 || clen == containerLenNil {
  2056. return 0
  2057. }
  2058. if clen < 0 {
  2059. // if unspecified, return 64 for bytes, ... 8 for uint64, ... and everything else
  2060. clen = 64 / unit
  2061. if clen > minLenIfUnset {
  2062. return clen
  2063. }
  2064. return minLenIfUnset
  2065. }
  2066. if unit <= 0 {
  2067. return clen
  2068. }
  2069. if maxlen <= 0 {
  2070. maxlen = maxMem / unit
  2071. }
  2072. if clen < maxlen {
  2073. return clen
  2074. }
  2075. return maxlen
  2076. }