You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

decode.go 45KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642
  1. // Copyright (c) Faye Amacker. All rights reserved.
  2. // Licensed under the MIT License. See LICENSE in the project root for license information.
  3. package cbor
  4. import (
  5. "encoding"
  6. "encoding/binary"
  7. "errors"
  8. "fmt"
  9. "io"
  10. "math"
  11. "reflect"
  12. "strconv"
  13. "strings"
  14. "time"
  15. "unicode/utf8"
  16. "github.com/x448/float16"
  17. )
  18. // Unmarshal parses the CBOR-encoded data and stores the result in the value
  19. // pointed to by v using the default decoding options. If v is nil or not a
  20. // pointer, Unmarshal returns an error.
  21. //
  22. // Unmarshal uses the inverse of the encodings that Marshal uses, allocating
  23. // maps, slices, and pointers as necessary, with the following additional rules:
  24. //
  25. // To unmarshal CBOR into a pointer, Unmarshal first handles the case of the
  26. // CBOR being the CBOR literal null. In that case, Unmarshal sets the pointer
  27. // to nil. Otherwise, Unmarshal unmarshals the CBOR into the value pointed at
  28. // by the pointer. If the pointer is nil, Unmarshal allocates a new value for
  29. // it to point to.
  30. //
  31. // To unmarshal CBOR into an interface value, Unmarshal stores one of these in
  32. // the interface value:
  33. //
  34. // bool, for CBOR booleans
  35. // uint64, for CBOR positive integers
  36. // int64, for CBOR negative integers
  37. // float64, for CBOR floating points
  38. // []byte, for CBOR byte strings
  39. // string, for CBOR text strings
  40. // []interface{}, for CBOR arrays
  41. // map[interface{}]interface{}, for CBOR maps
  42. // nil, for CBOR null
  43. //
  44. // To unmarshal a CBOR array into a slice, Unmarshal allocates a new slice only
  45. // if the CBOR array is empty or slice capacity is less than CBOR array length.
  46. // Otherwise Unmarshal reuses the existing slice, overwriting existing elements.
  47. // Unmarshal sets the slice length to CBOR array length.
  48. //
  49. // To ummarshal a CBOR array into a Go array, Unmarshal decodes CBOR array
  50. // elements into corresponding Go array elements. If the Go array is smaller
  51. // than the CBOR array, the additional CBOR array elements are discarded. If
  52. // the CBOR array is smaller than the Go array, the additional Go array elements
  53. // are set to zero values.
  54. //
  55. // To unmarshal a CBOR map into a map, Unmarshal allocates a new map only if the
  56. // map is nil. Otherwise Unmarshal reuses the existing map, keeping existing
  57. // entries. Unmarshal stores key-value pairs from the CBOR map into Go map.
  58. //
  59. // To unmarshal a CBOR map into a struct, Unmarshal matches CBOR map keys to the
  60. // keys in the following priority:
  61. //
  62. // 1. "cbor" key in struct field tag,
  63. // 2. "json" key in struct field tag,
  64. // 3. struct field name.
  65. //
  66. // Unmarshal prefers an exact match but also accepts a case-insensitive match.
  67. // Map keys which don't have a corresponding struct field are ignored.
  68. //
  69. // To unmarshal a CBOR text string into a time.Time value, Unmarshal parses text
  70. // string formatted in RFC3339. To unmarshal a CBOR integer/float into a
  71. // time.Time value, Unmarshal creates an unix time with integer/float as seconds
  72. // and fractional seconds since January 1, 1970 UTC.
  73. //
  74. // To unmarshal CBOR into a value implementing the Unmarshaler interface,
  75. // Unmarshal calls that value's UnmarshalCBOR method.
  76. //
  77. // Unmarshal decodes a CBOR byte string into a value implementing
  78. // encoding.BinaryUnmarshaler.
  79. //
  80. // If a CBOR value is not appropriate for a given Go type, or if a CBOR number
  81. // overflows the Go type, Unmarshal skips that field and completes the
  82. // unmarshalling as best as it can. If no more serious errors are encountered,
  83. // unmarshal returns an UnmarshalTypeError describing the earliest such error.
  84. // In any case, it's not guaranteed that all the remaining fields following the
  85. // problematic one will be unmarshaled into the target object.
  86. //
  87. // The CBOR null value unmarshals into a slice/map/pointer/interface by setting
  88. // that Go value to nil. Because null is often used to mean "not present",
  89. // unmarshalling a CBOR null into any other Go type has no effect on the value
  90. // produces no error.
  91. //
  92. // Unmarshal ignores CBOR tag data and parses tagged data following CBOR tag.
  93. func Unmarshal(data []byte, v interface{}) error {
  94. return defaultDecMode.Unmarshal(data, v)
  95. }
  96. // Unmarshaler is the interface implemented by types that can unmarshal a CBOR
  97. // representation of themselves. The input can be assumed to be a valid encoding
  98. // of a CBOR value. UnmarshalCBOR must copy the CBOR data if it wishes to retain
  99. // the data after returning.
  100. type Unmarshaler interface {
  101. UnmarshalCBOR([]byte) error
  102. }
  103. // InvalidUnmarshalError describes an invalid argument passed to Unmarshal.
  104. type InvalidUnmarshalError struct {
  105. Type reflect.Type
  106. }
  107. func (e *InvalidUnmarshalError) Error() string {
  108. if e.Type == nil {
  109. return "cbor: Unmarshal(nil)"
  110. }
  111. if e.Type.Kind() != reflect.Ptr {
  112. return "cbor: Unmarshal(non-pointer " + e.Type.String() + ")"
  113. }
  114. return "cbor: Unmarshal(nil " + e.Type.String() + ")"
  115. }
  116. // UnmarshalTypeError describes a CBOR value that was not appropriate for a Go type.
  117. type UnmarshalTypeError struct {
  118. Value string // description of CBOR value
  119. Type reflect.Type // type of Go value it could not be assigned to
  120. Struct string // struct type containing the field
  121. Field string // name of the field holding the Go value
  122. errMsg string // additional error message (optional)
  123. }
  124. func (e *UnmarshalTypeError) Error() string {
  125. var s string
  126. if e.Struct != "" || e.Field != "" {
  127. s = "cbor: cannot unmarshal " + e.Value + " into Go struct field " + e.Struct + "." + e.Field + " of type " + e.Type.String()
  128. } else {
  129. s = "cbor: cannot unmarshal " + e.Value + " into Go value of type " + e.Type.String()
  130. }
  131. if e.errMsg != "" {
  132. s += " (" + e.errMsg + ")"
  133. }
  134. return s
  135. }
  136. // DupMapKeyError describes detected duplicate map key in CBOR map.
  137. type DupMapKeyError struct {
  138. Key interface{}
  139. Index int
  140. }
  141. func (e *DupMapKeyError) Error() string {
  142. return fmt.Sprintf("cbor: found duplicate map key \"%v\" at map element index %d", e.Key, e.Index)
  143. }
  144. // DupMapKeyMode specifies how to enforce duplicate map key.
  145. type DupMapKeyMode int
  146. const (
  147. // DupMapKeyQuiet doesn't enforce duplicate map key. Decoder quietly (no error)
  148. // uses faster of "keep first" or "keep last" depending on Go data type and other factors.
  149. DupMapKeyQuiet DupMapKeyMode = iota
  150. // DupMapKeyEnforcedAPF enforces detection and rejection of duplicate map keys.
  151. // APF means "Allow Partial Fill" and the destination map or struct can be partially filled.
  152. // If a duplicate map key is detected, DupMapKeyError is returned without further decoding
  153. // of the map. It's the caller's responsibility to respond to DupMapKeyError by
  154. // discarding the partially filled result if their protocol requires it.
  155. // WARNING: using DupMapKeyEnforcedAPF will decrease performance and increase memory use.
  156. DupMapKeyEnforcedAPF
  157. maxDupMapKeyMode
  158. )
  159. func (dmkm DupMapKeyMode) valid() bool {
  160. return dmkm < maxDupMapKeyMode
  161. }
  162. // IndefLengthMode specifies whether to allow indefinite length items.
  163. type IndefLengthMode int
  164. const (
  165. // IndefLengthAllowed allows indefinite length items.
  166. IndefLengthAllowed IndefLengthMode = iota
  167. // IndefLengthForbidden disallows indefinite length items.
  168. IndefLengthForbidden
  169. maxIndefLengthMode
  170. )
  171. func (m IndefLengthMode) valid() bool {
  172. return m < maxIndefLengthMode
  173. }
  174. // TagsMode specifies whether to allow CBOR tags.
  175. type TagsMode int
  176. const (
  177. // TagsAllowed allows CBOR tags.
  178. TagsAllowed TagsMode = iota
  179. // TagsForbidden disallows CBOR tags.
  180. TagsForbidden
  181. maxTagsMode
  182. )
  183. func (tm TagsMode) valid() bool {
  184. return tm < maxTagsMode
  185. }
  186. // DecOptions specifies decoding options.
  187. type DecOptions struct {
  188. // DupMapKey specifies whether to enforce duplicate map key.
  189. DupMapKey DupMapKeyMode
  190. // TimeTag specifies whether to check validity of time.Time (e.g. valid tag number and tag content type).
  191. // For now, valid tag number means 0 or 1 as specified in RFC 7049 if the Go type is time.Time.
  192. TimeTag DecTagMode
  193. // MaxNestedLevels specifies the max nested levels allowed for any combination of CBOR array, maps, and tags.
  194. // Default is 32 levels and it can be set to [4, 256].
  195. MaxNestedLevels int
  196. // MaxArrayElements specifies the max number of elements for CBOR arrays.
  197. // Default is 128*1024=131072 and it can be set to [16, 134217728]
  198. MaxArrayElements int
  199. // MaxMapPairs specifies the max number of key-value pairs for CBOR maps.
  200. // Default is 128*1024=131072 and it can be set to [16, 134217728]
  201. MaxMapPairs int
  202. // IndefLength specifies whether to allow indefinite length CBOR items.
  203. IndefLength IndefLengthMode
  204. // TagsMd specifies whether to allow CBOR tags (major type 6).
  205. TagsMd TagsMode
  206. }
  207. // DecMode returns DecMode with immutable options and no tags (safe for concurrency).
  208. func (opts DecOptions) DecMode() (DecMode, error) {
  209. return opts.decMode()
  210. }
  211. // DecModeWithTags returns DecMode with options and tags that are both immutable (safe for concurrency).
  212. func (opts DecOptions) DecModeWithTags(tags TagSet) (DecMode, error) {
  213. if opts.TagsMd == TagsForbidden {
  214. return nil, errors.New("cbor: cannot create DecMode with TagSet when TagsMd is TagsForbidden")
  215. }
  216. if tags == nil {
  217. return nil, errors.New("cbor: cannot create DecMode with nil value as TagSet")
  218. }
  219. dm, err := opts.decMode()
  220. if err != nil {
  221. return nil, err
  222. }
  223. // Copy tags
  224. ts := tagSet(make(map[reflect.Type]*tagItem))
  225. syncTags := tags.(*syncTagSet)
  226. syncTags.RLock()
  227. for contentType, tag := range syncTags.t {
  228. if tag.opts.DecTag != DecTagIgnored {
  229. ts[contentType] = tag
  230. }
  231. }
  232. syncTags.RUnlock()
  233. if len(ts) > 0 {
  234. dm.tags = ts
  235. }
  236. return dm, nil
  237. }
  238. // DecModeWithSharedTags returns DecMode with immutable options and mutable shared tags (safe for concurrency).
  239. func (opts DecOptions) DecModeWithSharedTags(tags TagSet) (DecMode, error) {
  240. if opts.TagsMd == TagsForbidden {
  241. return nil, errors.New("cbor: cannot create DecMode with TagSet when TagsMd is TagsForbidden")
  242. }
  243. if tags == nil {
  244. return nil, errors.New("cbor: cannot create DecMode with nil value as TagSet")
  245. }
  246. dm, err := opts.decMode()
  247. if err != nil {
  248. return nil, err
  249. }
  250. dm.tags = tags
  251. return dm, nil
  252. }
  253. const (
  254. defaultMaxArrayElements = 131072
  255. minMaxArrayElements = 16
  256. maxMaxArrayElements = 134217728
  257. defaultMaxMapPairs = 131072
  258. minMaxMapPairs = 16
  259. maxMaxMapPairs = 134217728
  260. )
  261. func (opts DecOptions) decMode() (*decMode, error) {
  262. if !opts.DupMapKey.valid() {
  263. return nil, errors.New("cbor: invalid DupMapKey " + strconv.Itoa(int(opts.DupMapKey)))
  264. }
  265. if !opts.TimeTag.valid() {
  266. return nil, errors.New("cbor: invalid TimeTag " + strconv.Itoa(int(opts.TimeTag)))
  267. }
  268. if !opts.IndefLength.valid() {
  269. return nil, errors.New("cbor: invalid IndefLength " + strconv.Itoa(int(opts.IndefLength)))
  270. }
  271. if !opts.TagsMd.valid() {
  272. return nil, errors.New("cbor: invalid TagsMd " + strconv.Itoa(int(opts.TagsMd)))
  273. }
  274. if opts.MaxNestedLevels == 0 {
  275. opts.MaxNestedLevels = 32
  276. } else if opts.MaxNestedLevels < 4 || opts.MaxNestedLevels > 256 {
  277. return nil, errors.New("cbor: invalid MaxNestedLevels " + strconv.Itoa(opts.MaxNestedLevels) + " (range is [4, 256])")
  278. }
  279. if opts.MaxArrayElements == 0 {
  280. opts.MaxArrayElements = defaultMaxArrayElements
  281. } else if opts.MaxArrayElements < minMaxArrayElements || opts.MaxArrayElements > maxMaxArrayElements {
  282. return nil, errors.New("cbor: invalid MaxArrayElements " + strconv.Itoa(opts.MaxArrayElements) + " (range is [" + strconv.Itoa(minMaxArrayElements) + ", " + strconv.Itoa(maxMaxArrayElements) + "])")
  283. }
  284. if opts.MaxMapPairs == 0 {
  285. opts.MaxMapPairs = defaultMaxMapPairs
  286. } else if opts.MaxMapPairs < minMaxMapPairs || opts.MaxMapPairs > maxMaxMapPairs {
  287. return nil, errors.New("cbor: invalid MaxMapPairs " + strconv.Itoa(opts.MaxMapPairs) + " (range is [" + strconv.Itoa(minMaxMapPairs) + ", " + strconv.Itoa(maxMaxMapPairs) + "])")
  288. }
  289. dm := decMode{
  290. dupMapKey: opts.DupMapKey,
  291. timeTag: opts.TimeTag,
  292. maxNestedLevels: opts.MaxNestedLevels,
  293. maxArrayElements: opts.MaxArrayElements,
  294. maxMapPairs: opts.MaxMapPairs,
  295. indefLength: opts.IndefLength,
  296. tagsMd: opts.TagsMd,
  297. }
  298. return &dm, nil
  299. }
  300. // DecMode is the main interface for CBOR decoding.
  301. type DecMode interface {
  302. Unmarshal(data []byte, v interface{}) error
  303. NewDecoder(r io.Reader) *Decoder
  304. DecOptions() DecOptions
  305. }
  306. type decMode struct {
  307. tags tagProvider
  308. dupMapKey DupMapKeyMode
  309. timeTag DecTagMode
  310. maxNestedLevels int
  311. maxArrayElements int
  312. maxMapPairs int
  313. indefLength IndefLengthMode
  314. tagsMd TagsMode
  315. }
  316. var defaultDecMode, _ = DecOptions{}.decMode()
  317. // DecOptions returns user specified options used to create this DecMode.
  318. func (dm *decMode) DecOptions() DecOptions {
  319. return DecOptions{
  320. DupMapKey: dm.dupMapKey,
  321. TimeTag: dm.timeTag,
  322. MaxNestedLevels: dm.maxNestedLevels,
  323. MaxArrayElements: dm.maxArrayElements,
  324. MaxMapPairs: dm.maxMapPairs,
  325. IndefLength: dm.indefLength,
  326. TagsMd: dm.tagsMd,
  327. }
  328. }
  329. // Unmarshal parses the CBOR-encoded data and stores the result in the value
  330. // pointed to by v using dm DecMode. If v is nil or not a pointer, Unmarshal
  331. // returns an error.
  332. //
  333. // See the documentation for Unmarshal for details.
  334. func (dm *decMode) Unmarshal(data []byte, v interface{}) error {
  335. d := decodeState{data: data, dm: dm}
  336. return d.value(v)
  337. }
  338. // NewDecoder returns a new decoder that reads from r using dm DecMode.
  339. func (dm *decMode) NewDecoder(r io.Reader) *Decoder {
  340. return &Decoder{r: r, d: decodeState{dm: dm}}
  341. }
  342. type decodeState struct {
  343. data []byte
  344. off int // next read offset in data
  345. dm *decMode
  346. }
  347. func (d *decodeState) value(v interface{}) error {
  348. rv := reflect.ValueOf(v)
  349. if rv.Kind() != reflect.Ptr || rv.IsNil() {
  350. return &InvalidUnmarshalError{reflect.TypeOf(v)}
  351. }
  352. off := d.off // Save offset before data validation
  353. err := d.valid()
  354. d.off = off // Restore offset
  355. if err != nil {
  356. return err
  357. }
  358. rv = rv.Elem()
  359. if rv.Kind() == reflect.Interface && rv.NumMethod() == 0 {
  360. // Fast path to decode to empty interface without retrieving typeInfo.
  361. iv, err := d.parse()
  362. if iv != nil {
  363. rv.Set(reflect.ValueOf(iv))
  364. }
  365. return err
  366. }
  367. return d.parseToValue(rv, getTypeInfo(rv.Type()))
  368. }
  369. type cborType uint8
  370. const (
  371. cborTypePositiveInt cborType = 0x00
  372. cborTypeNegativeInt cborType = 0x20
  373. cborTypeByteString cborType = 0x40
  374. cborTypeTextString cborType = 0x60
  375. cborTypeArray cborType = 0x80
  376. cborTypeMap cborType = 0xa0
  377. cborTypeTag cborType = 0xc0
  378. cborTypePrimitives cborType = 0xe0
  379. )
  380. func (t cborType) String() string {
  381. switch t {
  382. case cborTypePositiveInt:
  383. return "positive integer"
  384. case cborTypeNegativeInt:
  385. return "negative integer"
  386. case cborTypeByteString:
  387. return "byte string"
  388. case cborTypeTextString:
  389. return "UTF-8 text string"
  390. case cborTypeArray:
  391. return "array"
  392. case cborTypeMap:
  393. return "map"
  394. case cborTypeTag:
  395. return "tag"
  396. case cborTypePrimitives:
  397. return "primitives"
  398. default:
  399. return "Invalid type " + strconv.Itoa(int(t))
  400. }
  401. }
  402. // parseToValue assumes data is well-formed, and does not perform bounds checking.
  403. // This function is complicated because it's the main function that decodes CBOR data to reflect.Value.
  404. func (d *decodeState) parseToValue(v reflect.Value, tInfo *typeInfo) error { //nolint:gocyclo
  405. // Create new value for the pointer v to point to if CBOR value is not nil/undefined.
  406. if !d.nextCBORNil() {
  407. for v.Kind() == reflect.Ptr {
  408. if v.IsNil() {
  409. if !v.CanSet() {
  410. d.skip()
  411. return errors.New("cbor: cannot set new value for " + v.Type().String())
  412. }
  413. v.Set(reflect.New(v.Type().Elem()))
  414. }
  415. v = v.Elem()
  416. }
  417. }
  418. if tInfo.spclType != specialTypeNone {
  419. switch tInfo.spclType {
  420. case specialTypeEmptyIface:
  421. iv, err := d.parse()
  422. if iv != nil {
  423. v.Set(reflect.ValueOf(iv))
  424. }
  425. return err
  426. case specialTypeTag:
  427. return d.parseToTag(v)
  428. case specialTypeTime:
  429. return d.parseToTime(v)
  430. case specialTypeUnmarshalerIface:
  431. return d.parseToUnmarshaler(v)
  432. }
  433. }
  434. // Check registered tag number
  435. if tagItem := d.getRegisteredTagItem(tInfo.nonPtrType); tagItem != nil {
  436. t := d.nextCBORType()
  437. if t != cborTypeTag {
  438. if tagItem.opts.DecTag == DecTagRequired {
  439. d.skip() // Required tag number is absent, skip entire tag
  440. return &UnmarshalTypeError{Value: t.String(), Type: tInfo.typ, errMsg: "expect CBOR tag value"}
  441. }
  442. } else if err := d.validRegisteredTagNums(tInfo.nonPtrType, tagItem.num); err != nil {
  443. d.skip() // Skip tag content
  444. return err
  445. }
  446. }
  447. t := d.nextCBORType()
  448. // Skip tag number(s) here to avoid recursion
  449. if t == cborTypeTag {
  450. d.getHead()
  451. t = d.nextCBORType()
  452. for t == cborTypeTag {
  453. d.getHead()
  454. t = d.nextCBORType()
  455. }
  456. }
  457. switch t {
  458. case cborTypePositiveInt:
  459. _, _, val := d.getHead()
  460. return fillPositiveInt(t, val, v)
  461. case cborTypeNegativeInt:
  462. _, _, val := d.getHead()
  463. if val > math.MaxInt64 {
  464. return &UnmarshalTypeError{
  465. Value: t.String(),
  466. Type: tInfo.nonPtrType,
  467. errMsg: "-1-" + strconv.FormatUint(val, 10) + " overflows Go's int64",
  468. }
  469. }
  470. nValue := int64(-1) ^ int64(val)
  471. return fillNegativeInt(t, nValue, v)
  472. case cborTypeByteString:
  473. b := d.parseByteString()
  474. return fillByteString(t, b, v)
  475. case cborTypeTextString:
  476. b, err := d.parseTextString()
  477. if err != nil {
  478. return err
  479. }
  480. return fillTextString(t, b, v)
  481. case cborTypePrimitives:
  482. _, ai, val := d.getHead()
  483. if ai < 20 || ai == 24 {
  484. return fillPositiveInt(t, val, v)
  485. }
  486. switch ai {
  487. case 20, 21:
  488. return fillBool(t, ai == 21, v)
  489. case 22, 23:
  490. return fillNil(t, v)
  491. case 25:
  492. f := float64(float16.Frombits(uint16(val)).Float32())
  493. return fillFloat(t, f, v)
  494. case 26:
  495. f := float64(math.Float32frombits(uint32(val)))
  496. return fillFloat(t, f, v)
  497. case 27:
  498. f := math.Float64frombits(val)
  499. return fillFloat(t, f, v)
  500. }
  501. case cborTypeArray:
  502. if tInfo.nonPtrKind == reflect.Slice {
  503. return d.parseArrayToSlice(v, tInfo)
  504. } else if tInfo.nonPtrKind == reflect.Array {
  505. return d.parseArrayToArray(v, tInfo)
  506. } else if tInfo.nonPtrKind == reflect.Struct {
  507. return d.parseArrayToStruct(v, tInfo)
  508. }
  509. d.skip()
  510. return &UnmarshalTypeError{Value: t.String(), Type: tInfo.nonPtrType}
  511. case cborTypeMap:
  512. if tInfo.nonPtrKind == reflect.Struct {
  513. return d.parseMapToStruct(v, tInfo)
  514. } else if tInfo.nonPtrKind == reflect.Map {
  515. return d.parseMapToMap(v, tInfo)
  516. }
  517. d.skip()
  518. return &UnmarshalTypeError{Value: t.String(), Type: tInfo.nonPtrType}
  519. }
  520. return nil
  521. }
  522. func (d *decodeState) parseToTag(v reflect.Value) error {
  523. t := d.nextCBORType()
  524. if t != cborTypeTag {
  525. d.skip()
  526. return &UnmarshalTypeError{Value: t.String(), Type: typeTag}
  527. }
  528. // Unmarshal tag number
  529. _, _, num := d.getHead()
  530. // Unmarshal tag content
  531. content, err := d.parse()
  532. if err != nil {
  533. return err
  534. }
  535. v.Set(reflect.ValueOf(Tag{num, content}))
  536. return nil
  537. }
  538. func (d *decodeState) parseToTime(v reflect.Value) error {
  539. t := d.nextCBORType()
  540. // Verify that tag number or absent of tag number is acceptable to specified timeTag.
  541. if t == cborTypeTag {
  542. if d.dm.timeTag == DecTagIgnored {
  543. // Skip tag number
  544. d.getHead()
  545. t = d.nextCBORType()
  546. for t == cborTypeTag {
  547. d.getHead()
  548. t = d.nextCBORType()
  549. }
  550. } else {
  551. // Read tag number
  552. _, _, tagNum := d.getHead()
  553. // Verify tag number (0 or 1) is followed by appropriate tag content type.
  554. t = d.nextCBORType()
  555. switch tagNum {
  556. case 0:
  557. // Tag content (date/time text string in RFC 3339 format) must be string type.
  558. if t != cborTypeTextString {
  559. d.skip()
  560. return errors.New("cbor: tag number 0 must be followed by text string, got " + t.String())
  561. }
  562. case 1:
  563. // Tag content (epoch date/time) must be uint, int, or float type.
  564. if t != cborTypePositiveInt && t != cborTypeNegativeInt && (d.data[d.off] < 0xf9 || d.data[d.off] > 0xfb) {
  565. d.skip()
  566. return errors.New("cbor: tag number 1 must be followed by integer or floating-point number, got " + t.String())
  567. }
  568. default:
  569. d.skip()
  570. return errors.New("cbor: wrong tag number for time.Time, got " + strconv.Itoa(int(tagNum)) + ", expect 0 or 1")
  571. }
  572. }
  573. } else {
  574. if d.dm.timeTag == DecTagRequired {
  575. d.skip()
  576. return &UnmarshalTypeError{Value: t.String(), Type: typeTime, errMsg: "expect CBOR tag value"}
  577. }
  578. }
  579. switch t {
  580. case cborTypePositiveInt:
  581. _, _, val := d.getHead()
  582. tm := time.Unix(int64(val), 0)
  583. v.Set(reflect.ValueOf(tm))
  584. return nil
  585. case cborTypeNegativeInt:
  586. _, _, val := d.getHead()
  587. if val > math.MaxInt64 {
  588. return &UnmarshalTypeError{
  589. Value: t.String(),
  590. Type: typeTime,
  591. errMsg: "-1-" + strconv.FormatUint(val, 10) + " overflows Go's int64",
  592. }
  593. }
  594. nValue := int64(-1) ^ int64(val)
  595. tm := time.Unix(nValue, 0)
  596. v.Set(reflect.ValueOf(tm))
  597. return nil
  598. case cborTypeTextString:
  599. b, err := d.parseTextString()
  600. if err != nil {
  601. return err
  602. }
  603. tm, err := time.Parse(time.RFC3339, string(b))
  604. if err != nil {
  605. return errors.New("cbor: cannot set " + string(b) + " for time.Time: " + err.Error())
  606. }
  607. v.Set(reflect.ValueOf(tm))
  608. return nil
  609. case cborTypePrimitives:
  610. _, ai, val := d.getHead()
  611. var f float64
  612. switch ai {
  613. case 22, 23:
  614. v.Set(reflect.ValueOf(time.Time{}))
  615. return nil
  616. case 25:
  617. f = float64(float16.Frombits(uint16(val)).Float32())
  618. case 26:
  619. f = float64(math.Float32frombits(uint32(val)))
  620. case 27:
  621. f = math.Float64frombits(val)
  622. default:
  623. return &UnmarshalTypeError{Value: t.String(), Type: typeTime}
  624. }
  625. if math.IsNaN(f) || math.IsInf(f, 0) {
  626. v.Set(reflect.ValueOf(time.Time{}))
  627. return nil
  628. }
  629. f1, f2 := math.Modf(f)
  630. tm := time.Unix(int64(f1), int64(f2*1e9))
  631. v.Set(reflect.ValueOf(tm))
  632. return nil
  633. }
  634. d.skip()
  635. return &UnmarshalTypeError{Value: t.String(), Type: typeTime}
  636. }
  637. // parseToUnmarshaler assumes data is well-formed, and does not perform bounds checking.
  638. func (d *decodeState) parseToUnmarshaler(v reflect.Value) error {
  639. if d.nextCBORNil() && v.Kind() == reflect.Ptr && v.IsNil() {
  640. d.skip()
  641. return nil
  642. }
  643. if v.Kind() != reflect.Ptr && v.CanAddr() {
  644. v = v.Addr()
  645. }
  646. if u, ok := v.Interface().(Unmarshaler); ok {
  647. start := d.off
  648. d.skip()
  649. return u.UnmarshalCBOR(d.data[start:d.off])
  650. }
  651. d.skip()
  652. return errors.New("cbor: failed to assert " + v.Type().String() + " as cbor.Unmarshaler")
  653. }
  654. // parse assumes data is well-formed, and does not perform bounds checking.
  655. func (d *decodeState) parse() (interface{}, error) {
  656. t := d.nextCBORType()
  657. switch t {
  658. case cborTypePositiveInt:
  659. _, _, val := d.getHead()
  660. return val, nil
  661. case cborTypeNegativeInt:
  662. _, _, val := d.getHead()
  663. if val > math.MaxInt64 {
  664. return nil, &UnmarshalTypeError{
  665. Value: t.String(),
  666. Type: reflect.TypeOf([]interface{}(nil)).Elem(),
  667. errMsg: "-1-" + strconv.FormatUint(val, 10) + " overflows Go's int64",
  668. }
  669. }
  670. nValue := int64(-1) ^ int64(val)
  671. return nValue, nil
  672. case cborTypeByteString:
  673. return d.parseByteString(), nil
  674. case cborTypeTextString:
  675. b, err := d.parseTextString()
  676. if err != nil {
  677. return nil, err
  678. }
  679. return string(b), nil
  680. case cborTypeTag:
  681. _, _, tagNum := d.getHead()
  682. nt := d.nextCBORType()
  683. content, err := d.parse()
  684. if err != nil {
  685. return nil, err
  686. }
  687. switch tagNum {
  688. case 0:
  689. // Tag content should be date/time text string in RFC 3339 format.
  690. s, ok := content.(string)
  691. if !ok {
  692. return nil, errors.New("cbor: tag number 0 must be followed by text string, got " + nt.String())
  693. }
  694. tm, err := time.Parse(time.RFC3339, s)
  695. if err != nil {
  696. return nil, errors.New("cbor: cannot set " + s + " for time.Time: " + err.Error())
  697. }
  698. return tm, nil
  699. case 1:
  700. // Tag content should be epoch date/time.
  701. switch content := content.(type) {
  702. case uint64:
  703. return time.Unix(int64(content), 0), nil
  704. case int64:
  705. return time.Unix(content, 0), nil
  706. case float64:
  707. f1, f2 := math.Modf(content)
  708. return time.Unix(int64(f1), int64(f2*1e9)), nil
  709. default:
  710. return nil, errors.New("cbor: tag number 1 must be followed by integer or floating-point number, got " + nt.String())
  711. }
  712. }
  713. return Tag{tagNum, content}, nil
  714. case cborTypePrimitives:
  715. _, ai, val := d.getHead()
  716. if ai < 20 || ai == 24 {
  717. return val, nil
  718. }
  719. switch ai {
  720. case 20, 21:
  721. return (ai == 21), nil
  722. case 22, 23:
  723. return nil, nil
  724. case 25:
  725. f := float64(float16.Frombits(uint16(val)).Float32())
  726. return f, nil
  727. case 26:
  728. f := float64(math.Float32frombits(uint32(val)))
  729. return f, nil
  730. case 27:
  731. f := math.Float64frombits(val)
  732. return f, nil
  733. }
  734. case cborTypeArray:
  735. return d.parseArray()
  736. case cborTypeMap:
  737. return d.parseMap()
  738. }
  739. return nil, nil
  740. }
  741. // parseByteString parses CBOR encoded byte string. It returns a byte slice
  742. // pointing to a copy of parsed data.
  743. func (d *decodeState) parseByteString() []byte {
  744. _, ai, val := d.getHead()
  745. if ai != 31 {
  746. b := make([]byte, int(val))
  747. copy(b, d.data[d.off:d.off+int(val)])
  748. d.off += int(val)
  749. return b
  750. }
  751. // Process indefinite length string chunks.
  752. b := []byte{}
  753. for !d.foundBreak() {
  754. _, _, val = d.getHead()
  755. b = append(b, d.data[d.off:d.off+int(val)]...)
  756. d.off += int(val)
  757. }
  758. return b
  759. }
  760. // parseTextString parses CBOR encoded text string. It does not return a string
  761. // to prevent creating an extra copy of string. Caller should wrap returned
  762. // byte slice as string when needed.
  763. //
  764. // parseStruct() uses parseTextString() to improve memory and performance,
  765. // compared with using parse(reflect.Value). parse(reflect.Value) sets
  766. // reflect.Value with parsed string, while parseTextString() returns zero-copy []byte.
  767. func (d *decodeState) parseTextString() ([]byte, error) {
  768. _, ai, val := d.getHead()
  769. if ai != 31 {
  770. b := d.data[d.off : d.off+int(val)]
  771. d.off += int(val)
  772. if !utf8.Valid(b) {
  773. return nil, &SemanticError{"cbor: invalid UTF-8 string"}
  774. }
  775. return b, nil
  776. }
  777. // Process indefinite length string chunks.
  778. b := []byte{}
  779. for !d.foundBreak() {
  780. _, _, val = d.getHead()
  781. x := d.data[d.off : d.off+int(val)]
  782. d.off += int(val)
  783. if !utf8.Valid(x) {
  784. for !d.foundBreak() {
  785. d.skip() // Skip remaining chunk on error
  786. }
  787. return nil, &SemanticError{"cbor: invalid UTF-8 string"}
  788. }
  789. b = append(b, x...)
  790. }
  791. return b, nil
  792. }
  793. func (d *decodeState) parseArray() ([]interface{}, error) {
  794. _, ai, val := d.getHead()
  795. hasSize := (ai != 31)
  796. count := int(val)
  797. if !hasSize {
  798. count = d.numOfItemsUntilBreak() // peek ahead to get array size to preallocate slice for better performance
  799. }
  800. v := make([]interface{}, count)
  801. var e interface{}
  802. var err, lastErr error
  803. for i := 0; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ {
  804. if e, lastErr = d.parse(); lastErr != nil {
  805. if err == nil {
  806. err = lastErr
  807. }
  808. continue
  809. }
  810. v[i] = e
  811. }
  812. return v, err
  813. }
  814. func (d *decodeState) parseArrayToSlice(v reflect.Value, tInfo *typeInfo) error {
  815. _, ai, val := d.getHead()
  816. hasSize := (ai != 31)
  817. count := int(val)
  818. if !hasSize {
  819. count = d.numOfItemsUntilBreak() // peek ahead to get array size to preallocate slice for better performance
  820. }
  821. if count == 0 {
  822. v.Set(reflect.MakeSlice(tInfo.nonPtrType, 0, 0))
  823. }
  824. if v.IsNil() || v.Cap() < count {
  825. v.Set(reflect.MakeSlice(tInfo.nonPtrType, count, count))
  826. }
  827. v.SetLen(count)
  828. var err error
  829. for i := 0; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ {
  830. if lastErr := d.parseToValue(v.Index(i), tInfo.elemTypeInfo); lastErr != nil {
  831. if err == nil {
  832. err = lastErr
  833. }
  834. }
  835. }
  836. return err
  837. }
  838. func (d *decodeState) parseArrayToArray(v reflect.Value, tInfo *typeInfo) error {
  839. _, ai, val := d.getHead()
  840. hasSize := (ai != 31)
  841. count := int(val)
  842. gi := 0
  843. vLen := v.Len()
  844. var err error
  845. for ci := 0; (hasSize && ci < count) || (!hasSize && !d.foundBreak()); ci++ {
  846. if gi < vLen {
  847. // Read CBOR array element and set array element
  848. if lastErr := d.parseToValue(v.Index(gi), tInfo.elemTypeInfo); lastErr != nil {
  849. if err == nil {
  850. err = lastErr
  851. }
  852. }
  853. gi++
  854. } else {
  855. d.skip() // Skip remaining CBOR array element
  856. }
  857. }
  858. // Set remaining Go array elements to zero values.
  859. if gi < vLen {
  860. zeroV := reflect.Zero(tInfo.elemTypeInfo.typ)
  861. for ; gi < vLen; gi++ {
  862. v.Index(gi).Set(zeroV)
  863. }
  864. }
  865. return err
  866. }
  867. func (d *decodeState) parseMap() (map[interface{}]interface{}, error) {
  868. _, ai, val := d.getHead()
  869. hasSize := (ai != 31)
  870. count := int(val)
  871. m := make(map[interface{}]interface{})
  872. var k, e interface{}
  873. var err, lastErr error
  874. keyCount := 0
  875. for i := 0; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ {
  876. // Parse CBOR map key.
  877. if k, lastErr = d.parse(); lastErr != nil {
  878. if err == nil {
  879. err = lastErr
  880. }
  881. d.skip()
  882. continue
  883. }
  884. // Detect if CBOR map key can be used as Go map key.
  885. kkind := reflect.ValueOf(k).Kind()
  886. if tag, ok := k.(Tag); ok {
  887. kkind = tag.contentKind()
  888. }
  889. if !isHashableKind(kkind) {
  890. if err == nil {
  891. err = errors.New("cbor: invalid map key type: " + kkind.String())
  892. }
  893. d.skip()
  894. continue
  895. }
  896. // Parse CBOR map value.
  897. if e, lastErr = d.parse(); lastErr != nil {
  898. if err == nil {
  899. err = lastErr
  900. }
  901. continue
  902. }
  903. // Add key-value pair to Go map.
  904. m[k] = e
  905. // Detect duplicate map key.
  906. if d.dm.dupMapKey == DupMapKeyEnforcedAPF {
  907. newKeyCount := len(m)
  908. if newKeyCount == keyCount {
  909. m[k] = nil
  910. err = &DupMapKeyError{k, i}
  911. i++
  912. // skip the rest of the map
  913. for ; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ {
  914. d.skip() // Skip map key
  915. d.skip() // Skip map value
  916. }
  917. return m, err
  918. }
  919. keyCount = newKeyCount
  920. }
  921. }
  922. return m, err
  923. }
  924. func (d *decodeState) parseMapToMap(v reflect.Value, tInfo *typeInfo) error { //nolint:gocyclo
  925. _, ai, val := d.getHead()
  926. hasSize := (ai != 31)
  927. count := int(val)
  928. if v.IsNil() {
  929. mapsize := count
  930. if !hasSize {
  931. mapsize = 0
  932. }
  933. v.Set(reflect.MakeMapWithSize(tInfo.nonPtrType, mapsize))
  934. }
  935. keyType, eleType := tInfo.keyTypeInfo.typ, tInfo.elemTypeInfo.typ
  936. reuseKey, reuseEle := isImmutableKind(tInfo.keyTypeInfo.kind), isImmutableKind(tInfo.elemTypeInfo.kind)
  937. var keyValue, eleValue, zeroKeyValue, zeroEleValue reflect.Value
  938. keyIsInterfaceType := keyType == typeIntf // If key type is interface{}, need to check if key value is hashable.
  939. var err, lastErr error
  940. keyCount := v.Len()
  941. var existingKeys map[interface{}]bool // Store existing map keys, used for detecting duplicate map key.
  942. if d.dm.dupMapKey == DupMapKeyEnforcedAPF {
  943. existingKeys = make(map[interface{}]bool, keyCount)
  944. if keyCount > 0 {
  945. vKeys := v.MapKeys()
  946. for i := 0; i < len(vKeys); i++ {
  947. existingKeys[vKeys[i].Interface()] = true
  948. }
  949. }
  950. }
  951. for i := 0; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ {
  952. // Parse CBOR map key.
  953. if !keyValue.IsValid() {
  954. keyValue = reflect.New(keyType).Elem()
  955. } else if !reuseKey {
  956. if !zeroKeyValue.IsValid() {
  957. zeroKeyValue = reflect.Zero(keyType)
  958. }
  959. keyValue.Set(zeroKeyValue)
  960. }
  961. if lastErr = d.parseToValue(keyValue, tInfo.keyTypeInfo); lastErr != nil {
  962. if err == nil {
  963. err = lastErr
  964. }
  965. d.skip()
  966. continue
  967. }
  968. // Detect if CBOR map key can be used as Go map key.
  969. if keyIsInterfaceType {
  970. kkind := keyValue.Elem().Kind()
  971. if keyValue.Elem().IsValid() {
  972. if tag, ok := keyValue.Elem().Interface().(Tag); ok {
  973. kkind = tag.contentKind()
  974. }
  975. }
  976. if !isHashableKind(kkind) {
  977. if err == nil {
  978. err = errors.New("cbor: invalid map key type: " + kkind.String())
  979. }
  980. d.skip()
  981. continue
  982. }
  983. }
  984. // Parse CBOR map value.
  985. if !eleValue.IsValid() {
  986. eleValue = reflect.New(eleType).Elem()
  987. } else if !reuseEle {
  988. if !zeroEleValue.IsValid() {
  989. zeroEleValue = reflect.Zero(eleType)
  990. }
  991. eleValue.Set(zeroEleValue)
  992. }
  993. if lastErr := d.parseToValue(eleValue, tInfo.elemTypeInfo); lastErr != nil {
  994. if err == nil {
  995. err = lastErr
  996. }
  997. continue
  998. }
  999. // Add key-value pair to Go map.
  1000. v.SetMapIndex(keyValue, eleValue)
  1001. // Detect duplicate map key.
  1002. if d.dm.dupMapKey == DupMapKeyEnforcedAPF {
  1003. newKeyCount := v.Len()
  1004. if newKeyCount == keyCount {
  1005. kvi := keyValue.Interface()
  1006. if !existingKeys[kvi] {
  1007. v.SetMapIndex(keyValue, reflect.New(eleType).Elem())
  1008. err = &DupMapKeyError{kvi, i}
  1009. i++
  1010. // skip the rest of the map
  1011. for ; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ {
  1012. d.skip() // skip map key
  1013. d.skip() // skip map value
  1014. }
  1015. return err
  1016. }
  1017. delete(existingKeys, kvi)
  1018. }
  1019. keyCount = newKeyCount
  1020. }
  1021. }
  1022. return err
  1023. }
  1024. func (d *decodeState) parseArrayToStruct(v reflect.Value, tInfo *typeInfo) error {
  1025. structType := getDecodingStructType(tInfo.nonPtrType)
  1026. if structType.err != nil {
  1027. return structType.err
  1028. }
  1029. if !structType.toArray {
  1030. t := d.nextCBORType()
  1031. d.skip()
  1032. return &UnmarshalTypeError{
  1033. Value: t.String(),
  1034. Type: tInfo.nonPtrType,
  1035. errMsg: "cannot decode CBOR array to struct without toarray option",
  1036. }
  1037. }
  1038. start := d.off
  1039. t, ai, val := d.getHead()
  1040. hasSize := (ai != 31)
  1041. count := int(val)
  1042. if !hasSize {
  1043. count = d.numOfItemsUntilBreak() // peek ahead to get array size
  1044. }
  1045. if count != len(structType.fields) {
  1046. d.off = start
  1047. d.skip()
  1048. return &UnmarshalTypeError{
  1049. Value: t.String(),
  1050. Type: tInfo.typ,
  1051. errMsg: "cannot decode CBOR array to struct with different number of elements",
  1052. }
  1053. }
  1054. var err error
  1055. for i := 0; (hasSize && i < count) || (!hasSize && !d.foundBreak()); i++ {
  1056. f := structType.fields[i]
  1057. fv, lastErr := fieldByIndex(v, f.idx)
  1058. if lastErr != nil {
  1059. if err == nil {
  1060. err = lastErr
  1061. }
  1062. d.skip()
  1063. continue
  1064. }
  1065. if lastErr := d.parseToValue(fv, f.typInfo); lastErr != nil {
  1066. if err == nil {
  1067. if typeError, ok := lastErr.(*UnmarshalTypeError); ok {
  1068. typeError.Struct = tInfo.typ.String()
  1069. typeError.Field = f.name
  1070. err = typeError
  1071. } else {
  1072. err = lastErr
  1073. }
  1074. }
  1075. }
  1076. }
  1077. return err
  1078. }
  1079. // parseMapToStruct needs to be fast so gocyclo can be ignored for now.
  1080. func (d *decodeState) parseMapToStruct(v reflect.Value, tInfo *typeInfo) error { //nolint:gocyclo
  1081. structType := getDecodingStructType(tInfo.nonPtrType)
  1082. if structType.err != nil {
  1083. return structType.err
  1084. }
  1085. if structType.toArray {
  1086. t := d.nextCBORType()
  1087. d.skip()
  1088. return &UnmarshalTypeError{
  1089. Value: t.String(),
  1090. Type: tInfo.nonPtrType,
  1091. errMsg: "cannot decode CBOR map to struct with toarray option",
  1092. }
  1093. }
  1094. foundFldIdx := make([]bool, len(structType.fields))
  1095. _, ai, val := d.getHead()
  1096. hasSize := (ai != 31)
  1097. count := int(val)
  1098. var err, lastErr error
  1099. keyCount := 0
  1100. var mapKeys map[interface{}]struct{} // Store map keys, used for detecting duplicate map key.
  1101. if d.dm.dupMapKey == DupMapKeyEnforcedAPF {
  1102. mapKeys = make(map[interface{}]struct{}, len(structType.fields))
  1103. }
  1104. for j := 0; (hasSize && j < count) || (!hasSize && !d.foundBreak()); j++ {
  1105. var f *field
  1106. var k interface{} // Used by duplicate map key detection
  1107. t := d.nextCBORType()
  1108. if t == cborTypeTextString {
  1109. var keyBytes []byte
  1110. keyBytes, lastErr = d.parseTextString()
  1111. if lastErr != nil {
  1112. if err == nil {
  1113. err = lastErr
  1114. }
  1115. d.skip() // skip value
  1116. continue
  1117. }
  1118. keyLen := len(keyBytes)
  1119. // Find field with exact match
  1120. for i := 0; i < len(structType.fields); i++ {
  1121. fld := structType.fields[i]
  1122. if !foundFldIdx[i] && len(fld.name) == keyLen && fld.name == string(keyBytes) {
  1123. f = fld
  1124. foundFldIdx[i] = true
  1125. break
  1126. }
  1127. }
  1128. // Find field with case-insensitive match
  1129. if f == nil {
  1130. keyString := string(keyBytes)
  1131. for i := 0; i < len(structType.fields); i++ {
  1132. fld := structType.fields[i]
  1133. if !foundFldIdx[i] && len(fld.name) == keyLen && strings.EqualFold(fld.name, keyString) {
  1134. f = fld
  1135. foundFldIdx[i] = true
  1136. break
  1137. }
  1138. }
  1139. }
  1140. if d.dm.dupMapKey == DupMapKeyEnforcedAPF {
  1141. k = string(keyBytes)
  1142. }
  1143. } else if t <= cborTypeNegativeInt { // uint/int
  1144. var nameAsInt int64
  1145. if t == cborTypePositiveInt {
  1146. _, _, val := d.getHead()
  1147. nameAsInt = int64(val)
  1148. } else {
  1149. _, _, val := d.getHead()
  1150. if val > math.MaxInt64 {
  1151. if err == nil {
  1152. err = &UnmarshalTypeError{
  1153. Value: t.String(),
  1154. Type: reflect.TypeOf(int64(0)),
  1155. errMsg: "-1-" + strconv.FormatUint(val, 10) + " overflows Go's int64",
  1156. }
  1157. }
  1158. d.skip() // skip value
  1159. continue
  1160. }
  1161. nameAsInt = int64(-1) ^ int64(val)
  1162. }
  1163. // Find field
  1164. for i := 0; i < len(structType.fields); i++ {
  1165. fld := structType.fields[i]
  1166. if !foundFldIdx[i] && fld.keyAsInt && fld.nameAsInt == nameAsInt {
  1167. f = fld
  1168. foundFldIdx[i] = true
  1169. break
  1170. }
  1171. }
  1172. if d.dm.dupMapKey == DupMapKeyEnforcedAPF {
  1173. k = nameAsInt
  1174. }
  1175. } else {
  1176. if err == nil {
  1177. err = &UnmarshalTypeError{
  1178. Value: t.String(),
  1179. Type: reflect.TypeOf(""),
  1180. errMsg: "map key is of type " + t.String() + " and cannot be used to match struct field name",
  1181. }
  1182. }
  1183. if d.dm.dupMapKey == DupMapKeyEnforcedAPF {
  1184. // parse key
  1185. k, lastErr = d.parse()
  1186. if lastErr != nil {
  1187. d.skip() // skip value
  1188. continue
  1189. }
  1190. // Detect if CBOR map key can be used as Go map key.
  1191. kkind := reflect.ValueOf(k).Kind()
  1192. if tag, ok := k.(Tag); ok {
  1193. kkind = tag.contentKind()
  1194. }
  1195. if !isHashableKind(kkind) {
  1196. d.skip() // skip value
  1197. continue
  1198. }
  1199. } else {
  1200. d.skip() // skip key
  1201. }
  1202. }
  1203. if d.dm.dupMapKey == DupMapKeyEnforcedAPF {
  1204. mapKeys[k] = struct{}{}
  1205. newKeyCount := len(mapKeys)
  1206. if newKeyCount == keyCount {
  1207. err = &DupMapKeyError{k, j}
  1208. d.skip() // skip value
  1209. j++
  1210. // skip the rest of the map
  1211. for ; (hasSize && j < count) || (!hasSize && !d.foundBreak()); j++ {
  1212. d.skip()
  1213. d.skip()
  1214. }
  1215. return err
  1216. }
  1217. keyCount = newKeyCount
  1218. }
  1219. if f == nil {
  1220. d.skip() // Skip value
  1221. continue
  1222. }
  1223. // reflect.Value.FieldByIndex() panics at nil pointer to unexported
  1224. // anonymous field. fieldByIndex() returns error.
  1225. fv, lastErr := fieldByIndex(v, f.idx)
  1226. if lastErr != nil {
  1227. if err == nil {
  1228. err = lastErr
  1229. }
  1230. d.skip()
  1231. continue
  1232. }
  1233. if lastErr = d.parseToValue(fv, f.typInfo); lastErr != nil {
  1234. if err == nil {
  1235. if typeError, ok := lastErr.(*UnmarshalTypeError); ok {
  1236. typeError.Struct = tInfo.nonPtrType.String()
  1237. typeError.Field = f.name
  1238. err = typeError
  1239. } else {
  1240. err = lastErr
  1241. }
  1242. }
  1243. }
  1244. }
  1245. return err
  1246. }
  1247. // validRegisteredTagNums verifies that tag numbers match registered tag numbers of type t.
  1248. // validRegisteredTagNums assumes next CBOR data type is tag. It scans all tag numbers, and stops at tag content.
  1249. func (d *decodeState) validRegisteredTagNums(t reflect.Type, registeredTagNums []uint64) error {
  1250. // Scan until next cbor data is tag content.
  1251. tagNums := make([]uint64, 0, 2)
  1252. for d.nextCBORType() == cborTypeTag {
  1253. _, _, val := d.getHead()
  1254. tagNums = append(tagNums, val)
  1255. }
  1256. // Verify that tag numbers match registered tag numbers of type t
  1257. if len(tagNums) != len(registeredTagNums) {
  1258. return &WrongTagError{t, registeredTagNums, tagNums}
  1259. }
  1260. for i, n := range registeredTagNums {
  1261. if n != tagNums[i] {
  1262. return &WrongTagError{t, registeredTagNums, tagNums}
  1263. }
  1264. }
  1265. return nil
  1266. }
  1267. func (d *decodeState) getRegisteredTagItem(vt reflect.Type) *tagItem {
  1268. if d.dm.tags != nil {
  1269. return d.dm.tags.get(vt)
  1270. }
  1271. return nil
  1272. }
  1273. // skip moves data offset to the next item. skip assumes data is well-formed,
  1274. // and does not perform bounds checking.
  1275. func (d *decodeState) skip() {
  1276. t, ai, val := d.getHead()
  1277. if ai == 31 {
  1278. switch t {
  1279. case cborTypeByteString, cborTypeTextString, cborTypeArray, cborTypeMap:
  1280. for {
  1281. if d.data[d.off] == 0xff {
  1282. d.off++
  1283. return
  1284. }
  1285. d.skip()
  1286. }
  1287. }
  1288. }
  1289. switch t {
  1290. case cborTypeByteString, cborTypeTextString:
  1291. d.off += int(val)
  1292. case cborTypeArray:
  1293. for i := 0; i < int(val); i++ {
  1294. d.skip()
  1295. }
  1296. case cborTypeMap:
  1297. for i := 0; i < int(val)*2; i++ {
  1298. d.skip()
  1299. }
  1300. case cborTypeTag:
  1301. d.skip()
  1302. }
  1303. }
  1304. // getHead assumes data is well-formed, and does not perform bounds checking.
  1305. func (d *decodeState) getHead() (t cborType, ai byte, val uint64) {
  1306. t = cborType(d.data[d.off] & 0xe0)
  1307. ai = d.data[d.off] & 0x1f
  1308. val = uint64(ai)
  1309. d.off++
  1310. if ai < 24 {
  1311. return
  1312. }
  1313. if ai == 24 {
  1314. val = uint64(d.data[d.off])
  1315. d.off++
  1316. return
  1317. }
  1318. if ai == 25 {
  1319. val = uint64(binary.BigEndian.Uint16(d.data[d.off : d.off+2]))
  1320. d.off += 2
  1321. return
  1322. }
  1323. if ai == 26 {
  1324. val = uint64(binary.BigEndian.Uint32(d.data[d.off : d.off+4]))
  1325. d.off += 4
  1326. return
  1327. }
  1328. if ai == 27 {
  1329. val = binary.BigEndian.Uint64(d.data[d.off : d.off+8])
  1330. d.off += 8
  1331. return
  1332. }
  1333. return
  1334. }
  1335. func (d *decodeState) numOfItemsUntilBreak() int {
  1336. savedOff := d.off
  1337. i := 0
  1338. for !d.foundBreak() {
  1339. d.skip()
  1340. i++
  1341. }
  1342. d.off = savedOff
  1343. return i
  1344. }
  1345. // foundBreak assumes data is well-formed, and does not perform bounds checking.
  1346. func (d *decodeState) foundBreak() bool {
  1347. if d.data[d.off] == 0xff {
  1348. d.off++
  1349. return true
  1350. }
  1351. return false
  1352. }
  1353. func (d *decodeState) reset(data []byte) {
  1354. d.data = data
  1355. d.off = 0
  1356. }
  1357. func (d *decodeState) nextCBORType() cborType {
  1358. return cborType(d.data[d.off] & 0xe0)
  1359. }
  1360. func (d *decodeState) nextCBORNil() bool {
  1361. return d.data[d.off] == 0xf6 || d.data[d.off] == 0xf7
  1362. }
  1363. var (
  1364. typeIntf = reflect.TypeOf([]interface{}(nil)).Elem()
  1365. typeTime = reflect.TypeOf(time.Time{})
  1366. typeUnmarshaler = reflect.TypeOf((*Unmarshaler)(nil)).Elem()
  1367. typeBinaryUnmarshaler = reflect.TypeOf((*encoding.BinaryUnmarshaler)(nil)).Elem()
  1368. )
  1369. func fillNil(t cborType, v reflect.Value) error {
  1370. switch v.Kind() {
  1371. case reflect.Slice, reflect.Map, reflect.Interface, reflect.Ptr:
  1372. v.Set(reflect.Zero(v.Type()))
  1373. return nil
  1374. }
  1375. return nil
  1376. }
  1377. func fillPositiveInt(t cborType, val uint64, v reflect.Value) error {
  1378. switch v.Kind() {
  1379. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1380. if val > math.MaxInt64 {
  1381. return &UnmarshalTypeError{Value: t.String(), Type: v.Type(), errMsg: strconv.FormatUint(val, 10) + " overflows " + v.Type().String()}
  1382. }
  1383. if v.OverflowInt(int64(val)) {
  1384. return &UnmarshalTypeError{Value: t.String(), Type: v.Type(), errMsg: strconv.FormatUint(val, 10) + " overflows " + v.Type().String()}
  1385. }
  1386. v.SetInt(int64(val))
  1387. return nil
  1388. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
  1389. if v.OverflowUint(val) {
  1390. return &UnmarshalTypeError{Value: t.String(), Type: v.Type(), errMsg: strconv.FormatUint(val, 10) + " overflows " + v.Type().String()}
  1391. }
  1392. v.SetUint(val)
  1393. return nil
  1394. case reflect.Float32, reflect.Float64:
  1395. f := float64(val)
  1396. v.SetFloat(f)
  1397. return nil
  1398. }
  1399. return &UnmarshalTypeError{Value: t.String(), Type: v.Type()}
  1400. }
  1401. func fillNegativeInt(t cborType, val int64, v reflect.Value) error {
  1402. switch v.Kind() {
  1403. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  1404. if v.OverflowInt(val) {
  1405. return &UnmarshalTypeError{Value: t.String(), Type: v.Type(), errMsg: strconv.FormatInt(val, 10) + " overflows " + v.Type().String()}
  1406. }
  1407. v.SetInt(val)
  1408. return nil
  1409. case reflect.Float32, reflect.Float64:
  1410. f := float64(val)
  1411. v.SetFloat(f)
  1412. return nil
  1413. }
  1414. return &UnmarshalTypeError{Value: t.String(), Type: v.Type()}
  1415. }
  1416. func fillBool(t cborType, val bool, v reflect.Value) error {
  1417. if v.Kind() == reflect.Bool {
  1418. v.SetBool(val)
  1419. return nil
  1420. }
  1421. return &UnmarshalTypeError{Value: t.String(), Type: v.Type()}
  1422. }
  1423. func fillFloat(t cborType, val float64, v reflect.Value) error {
  1424. switch v.Kind() {
  1425. case reflect.Float32, reflect.Float64:
  1426. if v.OverflowFloat(val) {
  1427. return &UnmarshalTypeError{
  1428. Value: t.String(),
  1429. Type: v.Type(),
  1430. errMsg: strconv.FormatFloat(val, 'E', -1, 64) + " overflows " + v.Type().String(),
  1431. }
  1432. }
  1433. v.SetFloat(val)
  1434. return nil
  1435. }
  1436. return &UnmarshalTypeError{Value: t.String(), Type: v.Type()}
  1437. }
  1438. func fillByteString(t cborType, val []byte, v reflect.Value) error {
  1439. if reflect.PtrTo(v.Type()).Implements(typeBinaryUnmarshaler) {
  1440. if v.CanAddr() {
  1441. v = v.Addr()
  1442. if u, ok := v.Interface().(encoding.BinaryUnmarshaler); ok {
  1443. return u.UnmarshalBinary(val)
  1444. }
  1445. }
  1446. return errors.New("cbor: cannot set new value for " + v.Type().String())
  1447. }
  1448. if v.Kind() == reflect.Slice && v.Type().Elem().Kind() == reflect.Uint8 {
  1449. v.SetBytes(val)
  1450. return nil
  1451. }
  1452. if v.Kind() == reflect.Array && v.Type().Elem().Kind() == reflect.Uint8 {
  1453. vLen := v.Len()
  1454. i := 0
  1455. for ; i < vLen && i < len(val); i++ {
  1456. v.Index(i).SetUint(uint64(val[i]))
  1457. }
  1458. // Set remaining Go array elements to zero values.
  1459. if i < vLen {
  1460. zeroV := reflect.Zero(reflect.TypeOf(byte(0)))
  1461. for ; i < vLen; i++ {
  1462. v.Index(i).Set(zeroV)
  1463. }
  1464. }
  1465. return nil
  1466. }
  1467. return &UnmarshalTypeError{Value: t.String(), Type: v.Type()}
  1468. }
  1469. func fillTextString(t cborType, val []byte, v reflect.Value) error {
  1470. if v.Kind() == reflect.String {
  1471. v.SetString(string(val))
  1472. return nil
  1473. }
  1474. return &UnmarshalTypeError{Value: t.String(), Type: v.Type()}
  1475. }
  1476. func isImmutableKind(k reflect.Kind) bool {
  1477. switch k {
  1478. case reflect.Bool,
  1479. reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
  1480. reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64,
  1481. reflect.Float32, reflect.Float64,
  1482. reflect.String:
  1483. return true
  1484. default:
  1485. return false
  1486. }
  1487. }
  1488. func isHashableKind(k reflect.Kind) bool {
  1489. switch k {
  1490. case reflect.Slice, reflect.Map, reflect.Func:
  1491. return false
  1492. default:
  1493. return true
  1494. }
  1495. }
  1496. // fieldByIndex returns the nested field corresponding to the index. It
  1497. // allocates pointer to struct field if it is nil and settable.
  1498. // reflect.Value.FieldByIndex() panics at nil pointer to unexported anonymous
  1499. // field. This function returns error.
  1500. func fieldByIndex(v reflect.Value, index []int) (reflect.Value, error) {
  1501. for _, i := range index {
  1502. if v.Kind() == reflect.Ptr && v.Type().Elem().Kind() == reflect.Struct {
  1503. if v.IsNil() {
  1504. if !v.CanSet() {
  1505. return reflect.Value{}, errors.New("cbor: cannot set embedded pointer to unexported struct: " + v.Type().String())
  1506. }
  1507. v.Set(reflect.New(v.Type().Elem()))
  1508. }
  1509. v = v.Elem()
  1510. }
  1511. v = v.Field(i)
  1512. }
  1513. return v, nil
  1514. }