decode.go 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. // BSON library for Go
  2. //
  3. // Copyright (c) 2010-2012 - Gustavo Niemeyer <gustavo@niemeyer.net>
  4. //
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are met:
  9. //
  10. // 1. Redistributions of source code must retain the above copyright notice, this
  11. // list of conditions and the following disclaimer.
  12. // 2. Redistributions in binary form must reproduce the above copyright notice,
  13. // this list of conditions and the following disclaimer in the documentation
  14. // and/or other materials provided with the distribution.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  17. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  20. // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. // gobson - BSON library for Go.
  27. package bson
  28. import (
  29. "errors"
  30. "fmt"
  31. "io"
  32. "math"
  33. "net/url"
  34. "reflect"
  35. "strconv"
  36. "sync"
  37. "time"
  38. )
  39. type decoder struct {
  40. in []byte
  41. i int
  42. docType reflect.Type
  43. }
  44. var typeM = reflect.TypeOf(M{})
  45. func newDecoder(in []byte) *decoder {
  46. return &decoder{in, 0, typeM}
  47. }
  48. // --------------------------------------------------------------------------
  49. // Some helper functions.
  50. func corrupted() {
  51. panic("Document is corrupted")
  52. }
  53. // --------------------------------------------------------------------------
  54. // Unmarshaling of documents.
  55. const (
  56. setterUnknown = iota
  57. setterNone
  58. setterType
  59. setterAddr
  60. )
  61. var setterStyles map[reflect.Type]int
  62. var setterIface reflect.Type
  63. var setterMutex sync.RWMutex
  64. func init() {
  65. var iface Setter
  66. setterIface = reflect.TypeOf(&iface).Elem()
  67. setterStyles = make(map[reflect.Type]int)
  68. }
  69. func setterStyle(outt reflect.Type) int {
  70. setterMutex.RLock()
  71. style := setterStyles[outt]
  72. setterMutex.RUnlock()
  73. if style != setterUnknown {
  74. return style
  75. }
  76. setterMutex.Lock()
  77. defer setterMutex.Unlock()
  78. if outt.Implements(setterIface) {
  79. style = setterType
  80. } else if reflect.PtrTo(outt).Implements(setterIface) {
  81. style = setterAddr
  82. } else {
  83. style = setterNone
  84. }
  85. setterStyles[outt] = style
  86. return style
  87. }
  88. func getSetter(outt reflect.Type, out reflect.Value) Setter {
  89. style := setterStyle(outt)
  90. if style == setterNone {
  91. return nil
  92. }
  93. if style == setterAddr {
  94. if !out.CanAddr() {
  95. return nil
  96. }
  97. out = out.Addr()
  98. } else if outt.Kind() == reflect.Ptr && out.IsNil() {
  99. out.Set(reflect.New(outt.Elem()))
  100. }
  101. return out.Interface().(Setter)
  102. }
  103. func clearMap(m reflect.Value) {
  104. var none reflect.Value
  105. for _, k := range m.MapKeys() {
  106. m.SetMapIndex(k, none)
  107. }
  108. }
  109. func (d *decoder) readDocTo(out reflect.Value) {
  110. var elemType reflect.Type
  111. outt := out.Type()
  112. outk := outt.Kind()
  113. for {
  114. if outk == reflect.Ptr && out.IsNil() {
  115. out.Set(reflect.New(outt.Elem()))
  116. }
  117. if setter := getSetter(outt, out); setter != nil {
  118. raw := d.readRaw(ElementDocument)
  119. err := setter.SetBSON(raw)
  120. if _, ok := err.(*TypeError); err != nil && !ok {
  121. panic(err)
  122. }
  123. return
  124. }
  125. if outk == reflect.Ptr {
  126. out = out.Elem()
  127. outt = out.Type()
  128. outk = out.Kind()
  129. continue
  130. }
  131. break
  132. }
  133. var fieldsMap map[string]fieldInfo
  134. var inlineMap reflect.Value
  135. if outt == typeRaw {
  136. out.Set(reflect.ValueOf(d.readRaw(ElementDocument)))
  137. return
  138. }
  139. origout := out
  140. if outk == reflect.Interface {
  141. if d.docType.Kind() == reflect.Map {
  142. mv := reflect.MakeMap(d.docType)
  143. out.Set(mv)
  144. out = mv
  145. } else {
  146. dv := reflect.New(d.docType).Elem()
  147. out.Set(dv)
  148. out = dv
  149. }
  150. outt = out.Type()
  151. outk = outt.Kind()
  152. }
  153. docType := d.docType
  154. keyType := typeString
  155. convertKey := false
  156. switch outk {
  157. case reflect.Map:
  158. keyType = outt.Key()
  159. if keyType != typeString {
  160. convertKey = true
  161. }
  162. elemType = outt.Elem()
  163. if elemType == typeIface {
  164. d.docType = outt
  165. }
  166. if out.IsNil() {
  167. out.Set(reflect.MakeMap(out.Type()))
  168. } else if out.Len() > 0 {
  169. clearMap(out)
  170. }
  171. case reflect.Struct:
  172. sinfo, err := getStructInfo(out.Type())
  173. if err != nil {
  174. panic(err)
  175. }
  176. fieldsMap = sinfo.FieldsMap
  177. out.Set(sinfo.Zero)
  178. if sinfo.InlineMap != -1 {
  179. inlineMap = out.Field(sinfo.InlineMap)
  180. if !inlineMap.IsNil() && inlineMap.Len() > 0 {
  181. clearMap(inlineMap)
  182. }
  183. elemType = inlineMap.Type().Elem()
  184. if elemType == typeIface {
  185. d.docType = inlineMap.Type()
  186. }
  187. }
  188. case reflect.Slice:
  189. switch outt.Elem() {
  190. case typeDocElem:
  191. origout.Set(d.readDocElems(outt))
  192. return
  193. case typeRawDocElem:
  194. origout.Set(d.readRawDocElems(outt))
  195. return
  196. }
  197. fallthrough
  198. default:
  199. panic("Unsupported document type for unmarshalling: " + out.Type().String())
  200. }
  201. end := int(d.readInt32())
  202. end += d.i - 4
  203. if end <= d.i || end > len(d.in) || d.in[end-1] != '\x00' {
  204. corrupted()
  205. }
  206. for d.in[d.i] != '\x00' {
  207. kind := d.readByte()
  208. name := d.readCStr()
  209. if d.i >= end {
  210. corrupted()
  211. }
  212. switch outk {
  213. case reflect.Map:
  214. e := reflect.New(elemType).Elem()
  215. if d.readElemTo(e, kind) {
  216. k := reflect.ValueOf(name)
  217. if convertKey {
  218. mapKeyType := out.Type().Key()
  219. mapKeyKind := mapKeyType.Kind()
  220. switch mapKeyKind {
  221. case reflect.Int:
  222. fallthrough
  223. case reflect.Int8:
  224. fallthrough
  225. case reflect.Int16:
  226. fallthrough
  227. case reflect.Int32:
  228. fallthrough
  229. case reflect.Int64:
  230. fallthrough
  231. case reflect.Uint:
  232. fallthrough
  233. case reflect.Uint8:
  234. fallthrough
  235. case reflect.Uint16:
  236. fallthrough
  237. case reflect.Uint32:
  238. fallthrough
  239. case reflect.Uint64:
  240. fallthrough
  241. case reflect.Float32:
  242. fallthrough
  243. case reflect.Float64:
  244. parsed := d.parseMapKeyAsFloat(k, mapKeyKind)
  245. k = reflect.ValueOf(parsed)
  246. case reflect.String:
  247. mapKeyType = keyType
  248. default:
  249. panic("BSON map must have string or decimal keys. Got: " + outt.String())
  250. }
  251. k = k.Convert(mapKeyType)
  252. }
  253. out.SetMapIndex(k, e)
  254. }
  255. case reflect.Struct:
  256. if info, ok := fieldsMap[name]; ok {
  257. if info.Inline == nil {
  258. d.readElemTo(out.Field(info.Num), kind)
  259. } else {
  260. d.readElemTo(out.FieldByIndex(info.Inline), kind)
  261. }
  262. } else if inlineMap.IsValid() {
  263. if inlineMap.IsNil() {
  264. inlineMap.Set(reflect.MakeMap(inlineMap.Type()))
  265. }
  266. e := reflect.New(elemType).Elem()
  267. if d.readElemTo(e, kind) {
  268. inlineMap.SetMapIndex(reflect.ValueOf(name), e)
  269. }
  270. } else {
  271. d.dropElem(kind)
  272. }
  273. case reflect.Slice:
  274. }
  275. if d.i >= end {
  276. corrupted()
  277. }
  278. }
  279. d.i++ // '\x00'
  280. if d.i != end {
  281. corrupted()
  282. }
  283. d.docType = docType
  284. }
  285. func (decoder) parseMapKeyAsFloat(k reflect.Value, mapKeyKind reflect.Kind) float64 {
  286. parsed, err := strconv.ParseFloat(k.String(), 64)
  287. if err != nil {
  288. panic("Map key is defined to be a decimal type (" + mapKeyKind.String() + ") but got error " +
  289. err.Error())
  290. }
  291. return parsed
  292. }
  293. func (d *decoder) readArrayDocTo(out reflect.Value) {
  294. end := int(d.readInt32())
  295. end += d.i - 4
  296. if end <= d.i || end > len(d.in) || d.in[end-1] != '\x00' {
  297. corrupted()
  298. }
  299. i := 0
  300. l := out.Len()
  301. for d.in[d.i] != '\x00' {
  302. if i >= l {
  303. panic("Length mismatch on array field")
  304. }
  305. kind := d.readByte()
  306. for d.i < end && d.in[d.i] != '\x00' {
  307. d.i++
  308. }
  309. if d.i >= end {
  310. corrupted()
  311. }
  312. d.i++
  313. d.readElemTo(out.Index(i), kind)
  314. if d.i >= end {
  315. corrupted()
  316. }
  317. i++
  318. }
  319. if i != l {
  320. panic("Length mismatch on array field")
  321. }
  322. d.i++ // '\x00'
  323. if d.i != end {
  324. corrupted()
  325. }
  326. }
  327. func (d *decoder) readSliceDoc(t reflect.Type) interface{} {
  328. tmp := make([]reflect.Value, 0, 8)
  329. elemType := t.Elem()
  330. if elemType == typeRawDocElem {
  331. d.dropElem(ElementArray)
  332. return reflect.Zero(t).Interface()
  333. }
  334. if elemType == typeRaw {
  335. return d.readSliceOfRaw()
  336. }
  337. end := int(d.readInt32())
  338. end += d.i - 4
  339. if end <= d.i || end > len(d.in) || d.in[end-1] != '\x00' {
  340. corrupted()
  341. }
  342. for d.in[d.i] != '\x00' {
  343. kind := d.readByte()
  344. for d.i < end && d.in[d.i] != '\x00' {
  345. d.i++
  346. }
  347. if d.i >= end {
  348. corrupted()
  349. }
  350. d.i++
  351. e := reflect.New(elemType).Elem()
  352. if d.readElemTo(e, kind) {
  353. tmp = append(tmp, e)
  354. }
  355. if d.i >= end {
  356. corrupted()
  357. }
  358. }
  359. d.i++ // '\x00'
  360. if d.i != end {
  361. corrupted()
  362. }
  363. n := len(tmp)
  364. slice := reflect.MakeSlice(t, n, n)
  365. for i := 0; i != n; i++ {
  366. slice.Index(i).Set(tmp[i])
  367. }
  368. return slice.Interface()
  369. }
  370. func BSONElementSize(kind byte, offset int, buffer []byte) (int, error) {
  371. switch kind {
  372. case ElementFloat64: // Float64
  373. return 8, nil
  374. case ElementJavaScriptWithoutScope: // JavaScript without scope
  375. fallthrough
  376. case ElementSymbol: // Symbol
  377. fallthrough
  378. case ElementString: // UTF-8 string
  379. size, err := getSize(offset, buffer)
  380. if err != nil {
  381. return 0, err
  382. }
  383. if size < 1 {
  384. return 0, errors.New("String size can't be less then one byte")
  385. }
  386. size += 4
  387. if offset+size > len(buffer) {
  388. return 0, io.ErrUnexpectedEOF
  389. }
  390. if buffer[offset+size-1] != 0 {
  391. return 0, errors.New("Invalid string: non zero-terminated")
  392. }
  393. return size, nil
  394. case ElementArray: // Array
  395. fallthrough
  396. case ElementDocument: // Document
  397. size, err := getSize(offset, buffer)
  398. if err != nil {
  399. return 0, err
  400. }
  401. if size < 5 {
  402. return 0, errors.New("Declared document size is too small")
  403. }
  404. return size, nil
  405. case ElementBinary: // Binary
  406. size, err := getSize(offset, buffer)
  407. if err != nil {
  408. return 0, err
  409. }
  410. if size < 0 {
  411. return 0, errors.New("Binary data size can't be negative")
  412. }
  413. return size + 5, nil
  414. case Element06: // Undefined (obsolete, but still seen in the wild)
  415. return 0, nil
  416. case ElementObjectId: // ObjectId
  417. return 12, nil
  418. case ElementBool: // Bool
  419. return 1, nil
  420. case ElementDatetime: // Timestamp
  421. return 8, nil
  422. case ElementNil: // Nil
  423. return 0, nil
  424. case ElementRegEx: // RegEx
  425. end := offset
  426. for i := 0; i < 2; i++ {
  427. for end < len(buffer) && buffer[end] != '\x00' {
  428. end++
  429. }
  430. end++
  431. }
  432. if end > len(buffer) {
  433. return 0, io.ErrUnexpectedEOF
  434. }
  435. return end - offset, nil
  436. case ElementDBPointer: // DBPointer
  437. size, err := getSize(offset, buffer)
  438. if err != nil {
  439. return 0, err
  440. }
  441. if size < 1 {
  442. return 0, errors.New("String size can't be less then one byte")
  443. }
  444. return size + 12 + 4, nil
  445. case ElementJavaScriptWithScope: // JavaScript with scope
  446. size, err := getSize(offset, buffer)
  447. if err != nil {
  448. return 0, err
  449. }
  450. if size < 4+5+5 {
  451. return 0, errors.New("Declared document element is too small")
  452. }
  453. return size, nil
  454. case ElementInt32: // Int32
  455. return 4, nil
  456. case ElementTimestamp: // Mongo-specific timestamp
  457. return 8, nil
  458. case ElementInt64: // Int64
  459. return 8, nil
  460. case ElementDecimal128: // Decimal128
  461. return 16, nil
  462. case ElementMaxKey: // Max key
  463. return 0, nil
  464. case ElementMinKey: // Min key
  465. return 0, nil
  466. default:
  467. return 0, errors.New(fmt.Sprintf("Unknown element kind (0x%02X)", kind))
  468. }
  469. }
  470. func (d *decoder) readRaw(kind byte) Raw {
  471. size, err := BSONElementSize(kind, d.i, d.in)
  472. if err != nil {
  473. corrupted()
  474. }
  475. if d.i+size > len(d.in) {
  476. corrupted()
  477. }
  478. d.i += size
  479. return Raw{
  480. Kind: kind,
  481. Data: d.in[d.i-size : d.i],
  482. }
  483. }
  484. func (d *decoder) readSliceOfRaw() interface{} {
  485. tmp := make([]Raw, 0, 8)
  486. end := int(d.readInt32())
  487. end += d.i - 4
  488. if end <= d.i || end > len(d.in) || d.in[end-1] != '\x00' {
  489. corrupted()
  490. }
  491. for d.in[d.i] != '\x00' {
  492. kind := d.readByte()
  493. for d.i < end && d.in[d.i] != '\x00' {
  494. d.i++
  495. }
  496. if d.i >= end {
  497. corrupted()
  498. }
  499. d.i++
  500. e := d.readRaw(kind)
  501. tmp = append(tmp, e)
  502. if d.i >= end {
  503. corrupted()
  504. }
  505. }
  506. d.i++ // '\x00'
  507. if d.i != end {
  508. corrupted()
  509. }
  510. return tmp
  511. }
  512. var typeSlice = reflect.TypeOf([]interface{}{})
  513. var typeIface = typeSlice.Elem()
  514. func (d *decoder) readDocElems(typ reflect.Type) reflect.Value {
  515. docType := d.docType
  516. d.docType = typ
  517. slice := make([]DocElem, 0, 8)
  518. d.readDocWith(func(kind byte, name string) {
  519. e := DocElem{Name: name}
  520. v := reflect.ValueOf(&e.Value)
  521. if d.readElemTo(v.Elem(), kind) {
  522. slice = append(slice, e)
  523. }
  524. })
  525. slicev := reflect.New(typ).Elem()
  526. slicev.Set(reflect.ValueOf(slice))
  527. d.docType = docType
  528. return slicev
  529. }
  530. func (d *decoder) readRawDocElems(typ reflect.Type) reflect.Value {
  531. docType := d.docType
  532. d.docType = typ
  533. slice := make([]RawDocElem, 0, 8)
  534. d.readDocWith(func(kind byte, name string) {
  535. e := RawDocElem{Name: name, Value: d.readRaw(kind)}
  536. slice = append(slice, e)
  537. })
  538. slicev := reflect.New(typ).Elem()
  539. slicev.Set(reflect.ValueOf(slice))
  540. d.docType = docType
  541. return slicev
  542. }
  543. func (d *decoder) readDocWith(f func(kind byte, name string)) {
  544. end := int(d.readInt32())
  545. end += d.i - 4
  546. if end <= d.i || end > len(d.in) || d.in[end-1] != '\x00' {
  547. corrupted()
  548. }
  549. for d.in[d.i] != '\x00' {
  550. kind := d.readByte()
  551. name := d.readCStr()
  552. if d.i >= end {
  553. corrupted()
  554. }
  555. f(kind, name)
  556. if d.i >= end {
  557. corrupted()
  558. }
  559. }
  560. d.i++ // '\x00'
  561. if d.i != end {
  562. corrupted()
  563. }
  564. }
  565. // --------------------------------------------------------------------------
  566. // Unmarshaling of individual elements within a document.
  567. func (d *decoder) dropElem(kind byte) {
  568. size, err := BSONElementSize(kind, d.i, d.in)
  569. if err != nil {
  570. corrupted()
  571. }
  572. if d.i+size > len(d.in) {
  573. corrupted()
  574. }
  575. d.i += size
  576. }
  577. // Attempt to decode an element from the document and put it into out.
  578. // If the types are not compatible, the returned ok value will be
  579. // false and out will be unchanged.
  580. func (d *decoder) readElemTo(out reflect.Value, kind byte) (good bool) {
  581. outt := out.Type()
  582. if outt == typeRaw {
  583. out.Set(reflect.ValueOf(d.readRaw(kind)))
  584. return true
  585. }
  586. if outt == typeRawPtr {
  587. raw := d.readRaw(kind)
  588. out.Set(reflect.ValueOf(&raw))
  589. return true
  590. }
  591. if kind == ElementDocument {
  592. // Delegate unmarshaling of documents.
  593. outt := out.Type()
  594. outk := out.Kind()
  595. switch outk {
  596. case reflect.Interface, reflect.Ptr, reflect.Struct, reflect.Map:
  597. d.readDocTo(out)
  598. return true
  599. }
  600. if setterStyle(outt) != setterNone {
  601. d.readDocTo(out)
  602. return true
  603. }
  604. if outk == reflect.Slice {
  605. switch outt.Elem() {
  606. case typeDocElem:
  607. out.Set(d.readDocElems(outt))
  608. case typeRawDocElem:
  609. out.Set(d.readRawDocElems(outt))
  610. default:
  611. d.dropElem(kind)
  612. }
  613. return true
  614. }
  615. d.dropElem(kind)
  616. return true
  617. }
  618. if setter := getSetter(outt, out); setter != nil {
  619. err := setter.SetBSON(d.readRaw(kind))
  620. if err == ErrSetZero {
  621. out.Set(reflect.Zero(outt))
  622. return true
  623. }
  624. if err == nil {
  625. return true
  626. }
  627. if _, ok := err.(*TypeError); !ok {
  628. panic(err)
  629. }
  630. return false
  631. }
  632. var in interface{}
  633. switch kind {
  634. case ElementFloat64:
  635. in = d.readFloat64()
  636. case ElementString:
  637. in = d.readStr()
  638. case ElementDocument:
  639. panic("Can't happen. Handled above.")
  640. case ElementArray:
  641. outt := out.Type()
  642. if setterStyle(outt) != setterNone {
  643. // Skip the value so its data is handed to the setter below.
  644. d.dropElem(kind)
  645. break
  646. }
  647. for outt.Kind() == reflect.Ptr {
  648. outt = outt.Elem()
  649. }
  650. switch outt.Kind() {
  651. case reflect.Array:
  652. d.readArrayDocTo(out)
  653. return true
  654. case reflect.Slice:
  655. in = d.readSliceDoc(outt)
  656. default:
  657. in = d.readSliceDoc(typeSlice)
  658. }
  659. case ElementBinary:
  660. b := d.readBinary()
  661. if b.Kind == BinaryGeneric || b.Kind == BinaryBinaryOld {
  662. in = b.Data
  663. } else {
  664. in = b
  665. }
  666. case Element06: // Undefined (obsolete, but still seen in the wild)
  667. in = Undefined
  668. case ElementObjectId:
  669. in = ObjectId(d.readBytes(12))
  670. case ElementBool:
  671. in = d.readBool()
  672. case ElementDatetime: // Timestamp
  673. // MongoDB handles timestamps as milliseconds.
  674. i := d.readInt64()
  675. if i == -62135596800000 {
  676. in = time.Time{} // In UTC for convenience.
  677. } else {
  678. in = time.Unix(i/1e3, i%1e3*1e6).UTC()
  679. }
  680. case ElementNil:
  681. in = nil
  682. case ElementRegEx:
  683. in = d.readRegEx()
  684. case ElementDBPointer:
  685. in = DBPointer{Namespace: d.readStr(), Id: ObjectId(d.readBytes(12))}
  686. case ElementJavaScriptWithoutScope:
  687. in = JavaScript{Code: d.readStr()}
  688. case ElementSymbol:
  689. in = Symbol(d.readStr())
  690. case ElementJavaScriptWithScope:
  691. start := d.i
  692. l := int(d.readInt32())
  693. js := JavaScript{d.readStr(), make(M)}
  694. d.readDocTo(reflect.ValueOf(js.Scope))
  695. if d.i != start+l {
  696. corrupted()
  697. }
  698. in = js
  699. case ElementInt32:
  700. in = int(d.readInt32())
  701. case ElementTimestamp: // Mongo-specific timestamp
  702. in = MongoTimestamp(d.readInt64())
  703. case ElementInt64:
  704. switch out.Type() {
  705. case typeTimeDuration:
  706. in = time.Duration(time.Duration(d.readInt64()) * time.Millisecond)
  707. default:
  708. in = d.readInt64()
  709. }
  710. case ElementDecimal128:
  711. in = Decimal128{
  712. l: uint64(d.readInt64()),
  713. h: uint64(d.readInt64()),
  714. }
  715. case ElementMaxKey:
  716. in = MaxKey
  717. case ElementMinKey:
  718. in = MinKey
  719. default:
  720. panic(fmt.Sprintf("Unknown element kind (0x%02X)", kind))
  721. }
  722. if in == nil {
  723. out.Set(reflect.Zero(outt))
  724. return true
  725. }
  726. outk := outt.Kind()
  727. // Dereference and initialize pointer if necessary.
  728. first := true
  729. for outk == reflect.Ptr {
  730. if !out.IsNil() {
  731. out = out.Elem()
  732. } else {
  733. elem := reflect.New(outt.Elem())
  734. if first {
  735. // Only set if value is compatible.
  736. first = false
  737. defer func(out, elem reflect.Value) {
  738. if good {
  739. out.Set(elem)
  740. }
  741. }(out, elem)
  742. } else {
  743. out.Set(elem)
  744. }
  745. out = elem
  746. }
  747. outt = out.Type()
  748. outk = outt.Kind()
  749. }
  750. inv := reflect.ValueOf(in)
  751. if outt == inv.Type() {
  752. out.Set(inv)
  753. return true
  754. }
  755. switch outk {
  756. case reflect.Interface:
  757. out.Set(inv)
  758. return true
  759. case reflect.String:
  760. switch inv.Kind() {
  761. case reflect.String:
  762. out.SetString(inv.String())
  763. return true
  764. case reflect.Slice:
  765. if b, ok := in.([]byte); ok {
  766. out.SetString(string(b))
  767. return true
  768. }
  769. case reflect.Int, reflect.Int64:
  770. if outt == typeJSONNumber {
  771. out.SetString(strconv.FormatInt(inv.Int(), 10))
  772. return true
  773. }
  774. case reflect.Float64:
  775. if outt == typeJSONNumber {
  776. out.SetString(strconv.FormatFloat(inv.Float(), 'f', -1, 64))
  777. return true
  778. }
  779. }
  780. case reflect.Slice, reflect.Array:
  781. // Remember, array (0x04) slices are built with the correct
  782. // element type. If we are here, must be a cross BSON kind
  783. // conversion (e.g. 0x05 unmarshalling on string).
  784. if outt.Elem().Kind() != reflect.Uint8 {
  785. break
  786. }
  787. switch inv.Kind() {
  788. case reflect.String:
  789. slice := []byte(inv.String())
  790. out.Set(reflect.ValueOf(slice))
  791. return true
  792. case reflect.Slice:
  793. switch outt.Kind() {
  794. case reflect.Array:
  795. reflect.Copy(out, inv)
  796. case reflect.Slice:
  797. out.SetBytes(inv.Bytes())
  798. }
  799. return true
  800. }
  801. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  802. switch inv.Kind() {
  803. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  804. out.SetInt(inv.Int())
  805. return true
  806. case reflect.Float32, reflect.Float64:
  807. out.SetInt(int64(inv.Float()))
  808. return true
  809. case reflect.Bool:
  810. if inv.Bool() {
  811. out.SetInt(1)
  812. } else {
  813. out.SetInt(0)
  814. }
  815. return true
  816. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  817. panic("can't happen: no uint types in BSON (!?)")
  818. }
  819. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  820. switch inv.Kind() {
  821. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  822. out.SetUint(uint64(inv.Int()))
  823. return true
  824. case reflect.Float32, reflect.Float64:
  825. out.SetUint(uint64(inv.Float()))
  826. return true
  827. case reflect.Bool:
  828. if inv.Bool() {
  829. out.SetUint(1)
  830. } else {
  831. out.SetUint(0)
  832. }
  833. return true
  834. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  835. panic("Can't happen. No uint types in BSON.")
  836. }
  837. case reflect.Float32, reflect.Float64:
  838. switch inv.Kind() {
  839. case reflect.Float32, reflect.Float64:
  840. out.SetFloat(inv.Float())
  841. return true
  842. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  843. out.SetFloat(float64(inv.Int()))
  844. return true
  845. case reflect.Bool:
  846. if inv.Bool() {
  847. out.SetFloat(1)
  848. } else {
  849. out.SetFloat(0)
  850. }
  851. return true
  852. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  853. panic("Can't happen. No uint types in BSON?")
  854. }
  855. case reflect.Bool:
  856. switch inv.Kind() {
  857. case reflect.Bool:
  858. out.SetBool(inv.Bool())
  859. return true
  860. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  861. out.SetBool(inv.Int() != 0)
  862. return true
  863. case reflect.Float32, reflect.Float64:
  864. out.SetBool(inv.Float() != 0)
  865. return true
  866. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  867. panic("Can't happen. No uint types in BSON?")
  868. }
  869. case reflect.Struct:
  870. if outt == typeURL && inv.Kind() == reflect.String {
  871. u, err := url.Parse(inv.String())
  872. if err != nil {
  873. panic(err)
  874. }
  875. out.Set(reflect.ValueOf(u).Elem())
  876. return true
  877. }
  878. if outt == typeBinary {
  879. if b, ok := in.([]byte); ok {
  880. out.Set(reflect.ValueOf(Binary{Data: b}))
  881. return true
  882. }
  883. }
  884. }
  885. return false
  886. }
  887. // --------------------------------------------------------------------------
  888. // Parsers of basic types.
  889. func (d *decoder) readRegEx() RegEx {
  890. re := RegEx{}
  891. re.Pattern = d.readCStr()
  892. re.Options = d.readCStr()
  893. return re
  894. }
  895. func (d *decoder) readBinary() Binary {
  896. l := d.readInt32()
  897. b := Binary{}
  898. b.Kind = d.readByte()
  899. if b.Kind == BinaryBinaryOld && l > 4 {
  900. // Weird obsolete format with redundant length.
  901. rl := d.readInt32()
  902. if rl != l-4 {
  903. corrupted()
  904. }
  905. l = rl
  906. }
  907. b.Data = d.readBytes(l)
  908. return b
  909. }
  910. func (d *decoder) readStr() string {
  911. l := d.readInt32()
  912. b := d.readBytes(l - 1)
  913. if d.readByte() != '\x00' {
  914. corrupted()
  915. }
  916. return string(b)
  917. }
  918. func (d *decoder) readCStr() string {
  919. start := d.i
  920. end := start
  921. l := len(d.in)
  922. for ; end != l; end++ {
  923. if d.in[end] == '\x00' {
  924. break
  925. }
  926. }
  927. d.i = end + 1
  928. if d.i > l {
  929. corrupted()
  930. }
  931. return string(d.in[start:end])
  932. }
  933. func (d *decoder) readBool() bool {
  934. b := d.readByte()
  935. if b == 0 {
  936. return false
  937. }
  938. if b == 1 {
  939. return true
  940. }
  941. panic(fmt.Sprintf("encoded boolean must be 1 or 0, found %d", b))
  942. }
  943. func (d *decoder) readFloat64() float64 {
  944. return math.Float64frombits(uint64(d.readInt64()))
  945. }
  946. func (d *decoder) readInt32() int32 {
  947. b := d.readBytes(4)
  948. return int32((uint32(b[0]) << 0) |
  949. (uint32(b[1]) << 8) |
  950. (uint32(b[2]) << 16) |
  951. (uint32(b[3]) << 24))
  952. }
  953. func getSize(offset int, b []byte) (int, error) {
  954. if offset+4 > len(b) {
  955. return 0, io.ErrUnexpectedEOF
  956. }
  957. return int((uint32(b[offset]) << 0) |
  958. (uint32(b[offset+1]) << 8) |
  959. (uint32(b[offset+2]) << 16) |
  960. (uint32(b[offset+3]) << 24)), nil
  961. }
  962. func (d *decoder) readInt64() int64 {
  963. b := d.readBytes(8)
  964. return int64((uint64(b[0]) << 0) |
  965. (uint64(b[1]) << 8) |
  966. (uint64(b[2]) << 16) |
  967. (uint64(b[3]) << 24) |
  968. (uint64(b[4]) << 32) |
  969. (uint64(b[5]) << 40) |
  970. (uint64(b[6]) << 48) |
  971. (uint64(b[7]) << 56))
  972. }
  973. func (d *decoder) readByte() byte {
  974. i := d.i
  975. d.i++
  976. if d.i > len(d.in) {
  977. corrupted()
  978. }
  979. return d.in[i]
  980. }
  981. func (d *decoder) readBytes(length int32) []byte {
  982. if length < 0 {
  983. corrupted()
  984. }
  985. start := d.i
  986. d.i += int(length)
  987. if d.i < start || d.i > len(d.in) {
  988. corrupted()
  989. }
  990. return d.in[start : start+int(length)]
  991. }