decoder.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. package maxminddb
  2. import (
  3. "encoding/binary"
  4. "math"
  5. "math/big"
  6. "reflect"
  7. "sync"
  8. )
  9. type decoder struct {
  10. buffer []byte
  11. }
  12. type dataType int
  13. const (
  14. _Extended dataType = iota
  15. _Pointer
  16. _String
  17. _Float64
  18. _Bytes
  19. _Uint16
  20. _Uint32
  21. _Map
  22. _Int32
  23. _Uint64
  24. _Uint128
  25. _Slice
  26. _Container
  27. _Marker
  28. _Bool
  29. _Float32
  30. )
  31. const (
  32. // This is the value used in libmaxminddb
  33. maximumDataStructureDepth = 512
  34. )
  35. func (d *decoder) decode(offset uint, result reflect.Value, depth int) (uint, error) {
  36. if depth > maximumDataStructureDepth {
  37. return 0, newInvalidDatabaseError("exceeded maximum data structure depth; database is likely corrupt")
  38. }
  39. typeNum, size, newOffset, err := d.decodeCtrlData(offset)
  40. if err != nil {
  41. return 0, err
  42. }
  43. if typeNum != _Pointer && result.Kind() == reflect.Uintptr {
  44. result.Set(reflect.ValueOf(uintptr(offset)))
  45. return d.nextValueOffset(offset, 1)
  46. }
  47. return d.decodeFromType(typeNum, size, newOffset, result, depth+1)
  48. }
  49. func (d *decoder) decodeCtrlData(offset uint) (dataType, uint, uint, error) {
  50. newOffset := offset + 1
  51. if offset >= uint(len(d.buffer)) {
  52. return 0, 0, 0, newOffsetError()
  53. }
  54. ctrlByte := d.buffer[offset]
  55. typeNum := dataType(ctrlByte >> 5)
  56. if typeNum == _Extended {
  57. if newOffset >= uint(len(d.buffer)) {
  58. return 0, 0, 0, newOffsetError()
  59. }
  60. typeNum = dataType(d.buffer[newOffset] + 7)
  61. newOffset++
  62. }
  63. var size uint
  64. size, newOffset, err := d.sizeFromCtrlByte(ctrlByte, newOffset, typeNum)
  65. return typeNum, size, newOffset, err
  66. }
  67. func (d *decoder) sizeFromCtrlByte(ctrlByte byte, offset uint, typeNum dataType) (uint, uint, error) {
  68. size := uint(ctrlByte & 0x1f)
  69. if typeNum == _Extended {
  70. return size, offset, nil
  71. }
  72. var bytesToRead uint
  73. if size < 29 {
  74. return size, offset, nil
  75. }
  76. bytesToRead = size - 28
  77. newOffset := offset + bytesToRead
  78. if newOffset > uint(len(d.buffer)) {
  79. return 0, 0, newOffsetError()
  80. }
  81. if size == 29 {
  82. return 29 + uint(d.buffer[offset]), offset + 1, nil
  83. }
  84. sizeBytes := d.buffer[offset:newOffset]
  85. switch {
  86. case size == 30:
  87. size = 285 + uintFromBytes(0, sizeBytes)
  88. case size > 30:
  89. size = uintFromBytes(0, sizeBytes) + 65821
  90. }
  91. return size, newOffset, nil
  92. }
  93. func (d *decoder) decodeFromType(
  94. dtype dataType,
  95. size uint,
  96. offset uint,
  97. result reflect.Value,
  98. depth int,
  99. ) (uint, error) {
  100. result = d.indirect(result)
  101. // For these types, size has a special meaning
  102. switch dtype {
  103. case _Bool:
  104. return d.unmarshalBool(size, offset, result)
  105. case _Map:
  106. return d.unmarshalMap(size, offset, result, depth)
  107. case _Pointer:
  108. return d.unmarshalPointer(size, offset, result, depth)
  109. case _Slice:
  110. return d.unmarshalSlice(size, offset, result, depth)
  111. }
  112. // For the remaining types, size is the byte size
  113. if offset+size > uint(len(d.buffer)) {
  114. return 0, newOffsetError()
  115. }
  116. switch dtype {
  117. case _Bytes:
  118. return d.unmarshalBytes(size, offset, result)
  119. case _Float32:
  120. return d.unmarshalFloat32(size, offset, result)
  121. case _Float64:
  122. return d.unmarshalFloat64(size, offset, result)
  123. case _Int32:
  124. return d.unmarshalInt32(size, offset, result)
  125. case _String:
  126. return d.unmarshalString(size, offset, result)
  127. case _Uint16:
  128. return d.unmarshalUint(size, offset, result, 16)
  129. case _Uint32:
  130. return d.unmarshalUint(size, offset, result, 32)
  131. case _Uint64:
  132. return d.unmarshalUint(size, offset, result, 64)
  133. case _Uint128:
  134. return d.unmarshalUint128(size, offset, result)
  135. default:
  136. return 0, newInvalidDatabaseError("unknown type: %d", dtype)
  137. }
  138. }
  139. func (d *decoder) unmarshalBool(size uint, offset uint, result reflect.Value) (uint, error) {
  140. if size > 1 {
  141. return 0, newInvalidDatabaseError("the MaxMind DB file's data section contains bad data (bool size of %v)", size)
  142. }
  143. value, newOffset, err := d.decodeBool(size, offset)
  144. if err != nil {
  145. return 0, err
  146. }
  147. switch result.Kind() {
  148. case reflect.Bool:
  149. result.SetBool(value)
  150. return newOffset, nil
  151. case reflect.Interface:
  152. if result.NumMethod() == 0 {
  153. result.Set(reflect.ValueOf(value))
  154. return newOffset, nil
  155. }
  156. }
  157. return newOffset, newUnmarshalTypeError(value, result.Type())
  158. }
  159. // indirect follows pointers and create values as necessary. This is
  160. // heavily based on encoding/json as my original version had a subtle
  161. // bug. This method should be considered to be licensed under
  162. // https://golang.org/LICENSE
  163. func (d *decoder) indirect(result reflect.Value) reflect.Value {
  164. for {
  165. // Load value from interface, but only if the result will be
  166. // usefully addressable.
  167. if result.Kind() == reflect.Interface && !result.IsNil() {
  168. e := result.Elem()
  169. if e.Kind() == reflect.Ptr && !e.IsNil() {
  170. result = e
  171. continue
  172. }
  173. }
  174. if result.Kind() != reflect.Ptr {
  175. break
  176. }
  177. if result.IsNil() {
  178. result.Set(reflect.New(result.Type().Elem()))
  179. }
  180. result = result.Elem()
  181. }
  182. return result
  183. }
  184. var sliceType = reflect.TypeOf([]byte{})
  185. func (d *decoder) unmarshalBytes(size uint, offset uint, result reflect.Value) (uint, error) {
  186. value, newOffset, err := d.decodeBytes(size, offset)
  187. if err != nil {
  188. return 0, err
  189. }
  190. switch result.Kind() {
  191. case reflect.Slice:
  192. if result.Type() == sliceType {
  193. result.SetBytes(value)
  194. return newOffset, nil
  195. }
  196. case reflect.Interface:
  197. if result.NumMethod() == 0 {
  198. result.Set(reflect.ValueOf(value))
  199. return newOffset, nil
  200. }
  201. }
  202. return newOffset, newUnmarshalTypeError(value, result.Type())
  203. }
  204. func (d *decoder) unmarshalFloat32(size uint, offset uint, result reflect.Value) (uint, error) {
  205. if size != 4 {
  206. return 0, newInvalidDatabaseError("the MaxMind DB file's data section contains bad data (float32 size of %v)", size)
  207. }
  208. value, newOffset, err := d.decodeFloat32(size, offset)
  209. if err != nil {
  210. return 0, err
  211. }
  212. switch result.Kind() {
  213. case reflect.Float32, reflect.Float64:
  214. result.SetFloat(float64(value))
  215. return newOffset, nil
  216. case reflect.Interface:
  217. if result.NumMethod() == 0 {
  218. result.Set(reflect.ValueOf(value))
  219. return newOffset, nil
  220. }
  221. }
  222. return newOffset, newUnmarshalTypeError(value, result.Type())
  223. }
  224. func (d *decoder) unmarshalFloat64(size uint, offset uint, result reflect.Value) (uint, error) {
  225. if size != 8 {
  226. return 0, newInvalidDatabaseError("the MaxMind DB file's data section contains bad data (float 64 size of %v)", size)
  227. }
  228. value, newOffset, err := d.decodeFloat64(size, offset)
  229. if err != nil {
  230. return 0, err
  231. }
  232. switch result.Kind() {
  233. case reflect.Float32, reflect.Float64:
  234. if result.OverflowFloat(value) {
  235. return 0, newUnmarshalTypeError(value, result.Type())
  236. }
  237. result.SetFloat(value)
  238. return newOffset, nil
  239. case reflect.Interface:
  240. if result.NumMethod() == 0 {
  241. result.Set(reflect.ValueOf(value))
  242. return newOffset, nil
  243. }
  244. }
  245. return newOffset, newUnmarshalTypeError(value, result.Type())
  246. }
  247. func (d *decoder) unmarshalInt32(size uint, offset uint, result reflect.Value) (uint, error) {
  248. if size > 4 {
  249. return 0, newInvalidDatabaseError("the MaxMind DB file's data section contains bad data (int32 size of %v)", size)
  250. }
  251. value, newOffset, err := d.decodeInt(size, offset)
  252. if err != nil {
  253. return 0, err
  254. }
  255. switch result.Kind() {
  256. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  257. n := int64(value)
  258. if !result.OverflowInt(n) {
  259. result.SetInt(n)
  260. return newOffset, nil
  261. }
  262. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  263. n := uint64(value)
  264. if !result.OverflowUint(n) {
  265. result.SetUint(n)
  266. return newOffset, nil
  267. }
  268. case reflect.Interface:
  269. if result.NumMethod() == 0 {
  270. result.Set(reflect.ValueOf(value))
  271. return newOffset, nil
  272. }
  273. }
  274. return newOffset, newUnmarshalTypeError(value, result.Type())
  275. }
  276. func (d *decoder) unmarshalMap(
  277. size uint,
  278. offset uint,
  279. result reflect.Value,
  280. depth int,
  281. ) (uint, error) {
  282. result = d.indirect(result)
  283. switch result.Kind() {
  284. default:
  285. return 0, newUnmarshalTypeError("map", result.Type())
  286. case reflect.Struct:
  287. return d.decodeStruct(size, offset, result, depth)
  288. case reflect.Map:
  289. return d.decodeMap(size, offset, result, depth)
  290. case reflect.Interface:
  291. if result.NumMethod() == 0 {
  292. rv := reflect.ValueOf(make(map[string]interface{}, size))
  293. newOffset, err := d.decodeMap(size, offset, rv, depth)
  294. result.Set(rv)
  295. return newOffset, err
  296. }
  297. return 0, newUnmarshalTypeError("map", result.Type())
  298. }
  299. }
  300. func (d *decoder) unmarshalPointer(size uint, offset uint, result reflect.Value, depth int) (uint, error) {
  301. pointer, newOffset, err := d.decodePointer(size, offset)
  302. if err != nil {
  303. return 0, err
  304. }
  305. _, err = d.decode(pointer, result, depth)
  306. return newOffset, err
  307. }
  308. func (d *decoder) unmarshalSlice(
  309. size uint,
  310. offset uint,
  311. result reflect.Value,
  312. depth int,
  313. ) (uint, error) {
  314. switch result.Kind() {
  315. case reflect.Slice:
  316. return d.decodeSlice(size, offset, result, depth)
  317. case reflect.Interface:
  318. if result.NumMethod() == 0 {
  319. a := []interface{}{}
  320. rv := reflect.ValueOf(&a).Elem()
  321. newOffset, err := d.decodeSlice(size, offset, rv, depth)
  322. result.Set(rv)
  323. return newOffset, err
  324. }
  325. }
  326. return 0, newUnmarshalTypeError("array", result.Type())
  327. }
  328. func (d *decoder) unmarshalString(size uint, offset uint, result reflect.Value) (uint, error) {
  329. value, newOffset, err := d.decodeString(size, offset)
  330. if err != nil {
  331. return 0, err
  332. }
  333. switch result.Kind() {
  334. case reflect.String:
  335. result.SetString(value)
  336. return newOffset, nil
  337. case reflect.Interface:
  338. if result.NumMethod() == 0 {
  339. result.Set(reflect.ValueOf(value))
  340. return newOffset, nil
  341. }
  342. }
  343. return newOffset, newUnmarshalTypeError(value, result.Type())
  344. }
  345. func (d *decoder) unmarshalUint(size uint, offset uint, result reflect.Value, uintType uint) (uint, error) {
  346. if size > uintType/8 {
  347. return 0, newInvalidDatabaseError("the MaxMind DB file's data section contains bad data (uint%v size of %v)", uintType, size)
  348. }
  349. value, newOffset, err := d.decodeUint(size, offset)
  350. if err != nil {
  351. return 0, err
  352. }
  353. switch result.Kind() {
  354. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  355. n := int64(value)
  356. if !result.OverflowInt(n) {
  357. result.SetInt(n)
  358. return newOffset, nil
  359. }
  360. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  361. if !result.OverflowUint(value) {
  362. result.SetUint(value)
  363. return newOffset, nil
  364. }
  365. case reflect.Interface:
  366. if result.NumMethod() == 0 {
  367. result.Set(reflect.ValueOf(value))
  368. return newOffset, nil
  369. }
  370. }
  371. return newOffset, newUnmarshalTypeError(value, result.Type())
  372. }
  373. var bigIntType = reflect.TypeOf(big.Int{})
  374. func (d *decoder) unmarshalUint128(size uint, offset uint, result reflect.Value) (uint, error) {
  375. if size > 16 {
  376. return 0, newInvalidDatabaseError("the MaxMind DB file's data section contains bad data (uint128 size of %v)", size)
  377. }
  378. value, newOffset, err := d.decodeUint128(size, offset)
  379. if err != nil {
  380. return 0, err
  381. }
  382. switch result.Kind() {
  383. case reflect.Struct:
  384. if result.Type() == bigIntType {
  385. result.Set(reflect.ValueOf(*value))
  386. return newOffset, nil
  387. }
  388. case reflect.Interface:
  389. if result.NumMethod() == 0 {
  390. result.Set(reflect.ValueOf(value))
  391. return newOffset, nil
  392. }
  393. }
  394. return newOffset, newUnmarshalTypeError(value, result.Type())
  395. }
  396. func (d *decoder) decodeBool(size uint, offset uint) (bool, uint, error) {
  397. return size != 0, offset, nil
  398. }
  399. func (d *decoder) decodeBytes(size uint, offset uint) ([]byte, uint, error) {
  400. newOffset := offset + size
  401. bytes := make([]byte, size)
  402. copy(bytes, d.buffer[offset:newOffset])
  403. return bytes, newOffset, nil
  404. }
  405. func (d *decoder) decodeFloat64(size uint, offset uint) (float64, uint, error) {
  406. newOffset := offset + size
  407. bits := binary.BigEndian.Uint64(d.buffer[offset:newOffset])
  408. return math.Float64frombits(bits), newOffset, nil
  409. }
  410. func (d *decoder) decodeFloat32(size uint, offset uint) (float32, uint, error) {
  411. newOffset := offset + size
  412. bits := binary.BigEndian.Uint32(d.buffer[offset:newOffset])
  413. return math.Float32frombits(bits), newOffset, nil
  414. }
  415. func (d *decoder) decodeInt(size uint, offset uint) (int, uint, error) {
  416. newOffset := offset + size
  417. var val int32
  418. for _, b := range d.buffer[offset:newOffset] {
  419. val = (val << 8) | int32(b)
  420. }
  421. return int(val), newOffset, nil
  422. }
  423. func (d *decoder) decodeMap(
  424. size uint,
  425. offset uint,
  426. result reflect.Value,
  427. depth int,
  428. ) (uint, error) {
  429. if result.IsNil() {
  430. result.Set(reflect.MakeMap(result.Type()))
  431. }
  432. for i := uint(0); i < size; i++ {
  433. var key []byte
  434. var err error
  435. key, offset, err = d.decodeKey(offset)
  436. if err != nil {
  437. return 0, err
  438. }
  439. value := reflect.New(result.Type().Elem())
  440. offset, err = d.decode(offset, value, depth)
  441. if err != nil {
  442. return 0, err
  443. }
  444. result.SetMapIndex(reflect.ValueOf(string(key)), value.Elem())
  445. }
  446. return offset, nil
  447. }
  448. func (d *decoder) decodePointer(
  449. size uint,
  450. offset uint,
  451. ) (uint, uint, error) {
  452. pointerSize := ((size >> 3) & 0x3) + 1
  453. newOffset := offset + pointerSize
  454. if newOffset > uint(len(d.buffer)) {
  455. return 0, 0, newOffsetError()
  456. }
  457. pointerBytes := d.buffer[offset:newOffset]
  458. var prefix uint
  459. if pointerSize == 4 {
  460. prefix = 0
  461. } else {
  462. prefix = uint(size & 0x7)
  463. }
  464. unpacked := uintFromBytes(prefix, pointerBytes)
  465. var pointerValueOffset uint
  466. switch pointerSize {
  467. case 1:
  468. pointerValueOffset = 0
  469. case 2:
  470. pointerValueOffset = 2048
  471. case 3:
  472. pointerValueOffset = 526336
  473. case 4:
  474. pointerValueOffset = 0
  475. }
  476. pointer := unpacked + pointerValueOffset
  477. return pointer, newOffset, nil
  478. }
  479. func (d *decoder) decodeSlice(
  480. size uint,
  481. offset uint,
  482. result reflect.Value,
  483. depth int,
  484. ) (uint, error) {
  485. result.Set(reflect.MakeSlice(result.Type(), int(size), int(size)))
  486. for i := 0; i < int(size); i++ {
  487. var err error
  488. offset, err = d.decode(offset, result.Index(i), depth)
  489. if err != nil {
  490. return 0, err
  491. }
  492. }
  493. return offset, nil
  494. }
  495. func (d *decoder) decodeString(size uint, offset uint) (string, uint, error) {
  496. newOffset := offset + size
  497. return string(d.buffer[offset:newOffset]), newOffset, nil
  498. }
  499. type fieldsType struct {
  500. namedFields map[string]int
  501. anonymousFields []int
  502. }
  503. var (
  504. fieldMap = map[reflect.Type]*fieldsType{}
  505. fieldMapMu sync.RWMutex
  506. )
  507. func (d *decoder) decodeStruct(
  508. size uint,
  509. offset uint,
  510. result reflect.Value,
  511. depth int,
  512. ) (uint, error) {
  513. resultType := result.Type()
  514. fieldMapMu.RLock()
  515. fields, ok := fieldMap[resultType]
  516. fieldMapMu.RUnlock()
  517. if !ok {
  518. numFields := resultType.NumField()
  519. namedFields := make(map[string]int, numFields)
  520. var anonymous []int
  521. for i := 0; i < numFields; i++ {
  522. field := resultType.Field(i)
  523. fieldName := field.Name
  524. if tag := field.Tag.Get("maxminddb"); tag != "" {
  525. if tag == "-" {
  526. continue
  527. }
  528. fieldName = tag
  529. }
  530. if field.Anonymous {
  531. anonymous = append(anonymous, i)
  532. continue
  533. }
  534. namedFields[fieldName] = i
  535. }
  536. fieldMapMu.Lock()
  537. fields = &fieldsType{namedFields, anonymous}
  538. fieldMap[resultType] = fields
  539. fieldMapMu.Unlock()
  540. }
  541. // This fills in embedded structs
  542. for _, i := range fields.anonymousFields {
  543. _, err := d.unmarshalMap(size, offset, result.Field(i), depth)
  544. if err != nil {
  545. return 0, err
  546. }
  547. }
  548. // This handles named fields
  549. for i := uint(0); i < size; i++ {
  550. var (
  551. err error
  552. key []byte
  553. )
  554. key, offset, err = d.decodeKey(offset)
  555. if err != nil {
  556. return 0, err
  557. }
  558. // The string() does not create a copy due to this compiler
  559. // optimization: https://github.com/golang/go/issues/3512
  560. j, ok := fields.namedFields[string(key)]
  561. if !ok {
  562. offset, err = d.nextValueOffset(offset, 1)
  563. if err != nil {
  564. return 0, err
  565. }
  566. continue
  567. }
  568. offset, err = d.decode(offset, result.Field(j), depth)
  569. if err != nil {
  570. return 0, err
  571. }
  572. }
  573. return offset, nil
  574. }
  575. func (d *decoder) decodeUint(size uint, offset uint) (uint64, uint, error) {
  576. newOffset := offset + size
  577. bytes := d.buffer[offset:newOffset]
  578. var val uint64
  579. for _, b := range bytes {
  580. val = (val << 8) | uint64(b)
  581. }
  582. return val, newOffset, nil
  583. }
  584. func (d *decoder) decodeUint128(size uint, offset uint) (*big.Int, uint, error) {
  585. newOffset := offset + size
  586. val := new(big.Int)
  587. val.SetBytes(d.buffer[offset:newOffset])
  588. return val, newOffset, nil
  589. }
  590. func uintFromBytes(prefix uint, uintBytes []byte) uint {
  591. val := prefix
  592. for _, b := range uintBytes {
  593. val = (val << 8) | uint(b)
  594. }
  595. return val
  596. }
  597. // decodeKey decodes a map key into []byte slice. We use a []byte so that we
  598. // can take advantage of https://github.com/golang/go/issues/3512 to avoid
  599. // copying the bytes when decoding a struct. Previously, we achieved this by
  600. // using unsafe.
  601. func (d *decoder) decodeKey(offset uint) ([]byte, uint, error) {
  602. typeNum, size, dataOffset, err := d.decodeCtrlData(offset)
  603. if err != nil {
  604. return nil, 0, err
  605. }
  606. if typeNum == _Pointer {
  607. pointer, ptrOffset, err := d.decodePointer(size, dataOffset)
  608. if err != nil {
  609. return nil, 0, err
  610. }
  611. key, _, err := d.decodeKey(pointer)
  612. return key, ptrOffset, err
  613. }
  614. if typeNum != _String {
  615. return nil, 0, newInvalidDatabaseError("unexpected type when decoding string: %v", typeNum)
  616. }
  617. newOffset := dataOffset + size
  618. if newOffset > uint(len(d.buffer)) {
  619. return nil, 0, newOffsetError()
  620. }
  621. return d.buffer[dataOffset:newOffset], newOffset, nil
  622. }
  623. // This function is used to skip ahead to the next value without decoding
  624. // the one at the offset passed in. The size bits have different meanings for
  625. // different data types
  626. func (d *decoder) nextValueOffset(offset uint, numberToSkip uint) (uint, error) {
  627. if numberToSkip == 0 {
  628. return offset, nil
  629. }
  630. typeNum, size, offset, err := d.decodeCtrlData(offset)
  631. if err != nil {
  632. return 0, err
  633. }
  634. switch typeNum {
  635. case _Pointer:
  636. _, offset, err = d.decodePointer(size, offset)
  637. if err != nil {
  638. return 0, err
  639. }
  640. case _Map:
  641. numberToSkip += 2 * size
  642. case _Slice:
  643. numberToSkip += size
  644. case _Bool:
  645. default:
  646. offset += size
  647. }
  648. return d.nextValueOffset(offset, numberToSkip-1)
  649. }