decode.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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. "fmt"
  30. "math"
  31. "net/url"
  32. "reflect"
  33. "sync"
  34. "time"
  35. )
  36. type decoder struct {
  37. in []byte
  38. i int
  39. docType reflect.Type
  40. }
  41. var typeM = reflect.TypeOf(M{})
  42. func newDecoder(in []byte) *decoder {
  43. return &decoder{in, 0, typeM}
  44. }
  45. // --------------------------------------------------------------------------
  46. // Some helper functions.
  47. func corrupted() {
  48. panic("Document is corrupted")
  49. }
  50. func settableValueOf(i interface{}) reflect.Value {
  51. v := reflect.ValueOf(i)
  52. sv := reflect.New(v.Type()).Elem()
  53. sv.Set(v)
  54. return sv
  55. }
  56. // --------------------------------------------------------------------------
  57. // Unmarshaling of documents.
  58. const (
  59. setterUnknown = iota
  60. setterNone
  61. setterType
  62. setterAddr
  63. )
  64. var setterStyle map[reflect.Type]int
  65. var setterIface reflect.Type
  66. var setterMutex sync.RWMutex
  67. func init() {
  68. var iface Setter
  69. setterIface = reflect.TypeOf(&iface).Elem()
  70. setterStyle = make(map[reflect.Type]int)
  71. }
  72. func getSetter(outt reflect.Type, out reflect.Value) Setter {
  73. setterMutex.RLock()
  74. style := setterStyle[outt]
  75. setterMutex.RUnlock()
  76. if style == setterNone {
  77. return nil
  78. }
  79. if style == setterUnknown {
  80. setterMutex.Lock()
  81. defer setterMutex.Unlock()
  82. if outt.Implements(setterIface) {
  83. setterStyle[outt] = setterType
  84. } else if reflect.PtrTo(outt).Implements(setterIface) {
  85. setterStyle[outt] = setterAddr
  86. } else {
  87. setterStyle[outt] = setterNone
  88. return nil
  89. }
  90. style = setterStyle[outt]
  91. }
  92. if style == setterAddr {
  93. if !out.CanAddr() {
  94. return nil
  95. }
  96. out = out.Addr()
  97. } else if outt.Kind() == reflect.Ptr && out.IsNil() {
  98. out.Set(reflect.New(outt.Elem()))
  99. }
  100. return out.Interface().(Setter)
  101. }
  102. func clearMap(m reflect.Value) {
  103. var none reflect.Value
  104. for _, k := range m.MapKeys() {
  105. m.SetMapIndex(k, none)
  106. }
  107. }
  108. func (d *decoder) readDocTo(out reflect.Value) {
  109. var elemType reflect.Type
  110. outt := out.Type()
  111. outk := outt.Kind()
  112. for {
  113. if outk == reflect.Ptr && out.IsNil() {
  114. out.Set(reflect.New(outt.Elem()))
  115. }
  116. if setter := getSetter(outt, out); setter != nil {
  117. var raw Raw
  118. d.readDocTo(reflect.ValueOf(&raw))
  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. start := d.i
  136. origout := out
  137. if outk == reflect.Interface {
  138. if d.docType.Kind() == reflect.Map {
  139. mv := reflect.MakeMap(d.docType)
  140. out.Set(mv)
  141. out = mv
  142. } else {
  143. dv := reflect.New(d.docType).Elem()
  144. out.Set(dv)
  145. out = dv
  146. }
  147. outt = out.Type()
  148. outk = outt.Kind()
  149. }
  150. docType := d.docType
  151. keyType := typeString
  152. convertKey := false
  153. switch outk {
  154. case reflect.Map:
  155. keyType = outt.Key()
  156. if keyType.Kind() != reflect.String {
  157. panic("BSON map must have string keys. Got: " + outt.String())
  158. }
  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. if outt != typeRaw {
  173. sinfo, err := getStructInfo(out.Type())
  174. if err != nil {
  175. panic(err)
  176. }
  177. fieldsMap = sinfo.FieldsMap
  178. out.Set(sinfo.Zero)
  179. if sinfo.InlineMap != -1 {
  180. inlineMap = out.Field(sinfo.InlineMap)
  181. if !inlineMap.IsNil() && inlineMap.Len() > 0 {
  182. clearMap(inlineMap)
  183. }
  184. elemType = inlineMap.Type().Elem()
  185. if elemType == typeIface {
  186. d.docType = inlineMap.Type()
  187. }
  188. }
  189. }
  190. case reflect.Slice:
  191. switch outt.Elem() {
  192. case typeDocElem:
  193. origout.Set(d.readDocElems(outt))
  194. return
  195. case typeRawDocElem:
  196. origout.Set(d.readRawDocElems(outt))
  197. return
  198. }
  199. fallthrough
  200. default:
  201. panic("Unsupported document type for unmarshalling: " + out.Type().String())
  202. }
  203. end := int(d.readInt32())
  204. end += d.i - 4
  205. if end <= d.i || end > len(d.in) || d.in[end-1] != '\x00' {
  206. corrupted()
  207. }
  208. for d.in[d.i] != '\x00' {
  209. kind := d.readByte()
  210. name := d.readCStr()
  211. if d.i >= end {
  212. corrupted()
  213. }
  214. switch outk {
  215. case reflect.Map:
  216. e := reflect.New(elemType).Elem()
  217. if d.readElemTo(e, kind) {
  218. k := reflect.ValueOf(name)
  219. if convertKey {
  220. k = k.Convert(keyType)
  221. }
  222. out.SetMapIndex(k, e)
  223. }
  224. case reflect.Struct:
  225. if outt == typeRaw {
  226. d.dropElem(kind)
  227. } else {
  228. if info, ok := fieldsMap[name]; ok {
  229. if info.Inline == nil {
  230. d.readElemTo(out.Field(info.Num), kind)
  231. } else {
  232. d.readElemTo(out.FieldByIndex(info.Inline), kind)
  233. }
  234. } else if inlineMap.IsValid() {
  235. if inlineMap.IsNil() {
  236. inlineMap.Set(reflect.MakeMap(inlineMap.Type()))
  237. }
  238. e := reflect.New(elemType).Elem()
  239. if d.readElemTo(e, kind) {
  240. inlineMap.SetMapIndex(reflect.ValueOf(name), e)
  241. }
  242. } else {
  243. d.dropElem(kind)
  244. }
  245. }
  246. case reflect.Slice:
  247. }
  248. if d.i >= end {
  249. corrupted()
  250. }
  251. }
  252. d.i++ // '\x00'
  253. if d.i != end {
  254. corrupted()
  255. }
  256. d.docType = docType
  257. if outt == typeRaw {
  258. out.Set(reflect.ValueOf(Raw{0x03, d.in[start:d.i]}))
  259. }
  260. }
  261. func (d *decoder) readArrayDocTo(out reflect.Value) {
  262. end := int(d.readInt32())
  263. end += d.i - 4
  264. if end <= d.i || end > len(d.in) || d.in[end-1] != '\x00' {
  265. corrupted()
  266. }
  267. i := 0
  268. l := out.Len()
  269. for d.in[d.i] != '\x00' {
  270. if i >= l {
  271. panic("Length mismatch on array field")
  272. }
  273. kind := d.readByte()
  274. for d.i < end && d.in[d.i] != '\x00' {
  275. d.i++
  276. }
  277. if d.i >= end {
  278. corrupted()
  279. }
  280. d.i++
  281. d.readElemTo(out.Index(i), kind)
  282. if d.i >= end {
  283. corrupted()
  284. }
  285. i++
  286. }
  287. if i != l {
  288. panic("Length mismatch on array field")
  289. }
  290. d.i++ // '\x00'
  291. if d.i != end {
  292. corrupted()
  293. }
  294. }
  295. func (d *decoder) readSliceDoc(t reflect.Type) interface{} {
  296. tmp := make([]reflect.Value, 0, 8)
  297. elemType := t.Elem()
  298. end := int(d.readInt32())
  299. end += d.i - 4
  300. if end <= d.i || end > len(d.in) || d.in[end-1] != '\x00' {
  301. corrupted()
  302. }
  303. for d.in[d.i] != '\x00' {
  304. kind := d.readByte()
  305. for d.i < end && d.in[d.i] != '\x00' {
  306. d.i++
  307. }
  308. if d.i >= end {
  309. corrupted()
  310. }
  311. d.i++
  312. e := reflect.New(elemType).Elem()
  313. if d.readElemTo(e, kind) {
  314. tmp = append(tmp, e)
  315. }
  316. if d.i >= end {
  317. corrupted()
  318. }
  319. }
  320. d.i++ // '\x00'
  321. if d.i != end {
  322. corrupted()
  323. }
  324. n := len(tmp)
  325. slice := reflect.MakeSlice(t, n, n)
  326. for i := 0; i != n; i++ {
  327. slice.Index(i).Set(tmp[i])
  328. }
  329. return slice.Interface()
  330. }
  331. var typeSlice = reflect.TypeOf([]interface{}{})
  332. var typeIface = typeSlice.Elem()
  333. func (d *decoder) readDocElems(typ reflect.Type) reflect.Value {
  334. docType := d.docType
  335. d.docType = typ
  336. slice := make([]DocElem, 0, 8)
  337. d.readDocWith(func(kind byte, name string) {
  338. e := DocElem{Name: name}
  339. v := reflect.ValueOf(&e.Value)
  340. if d.readElemTo(v.Elem(), kind) {
  341. slice = append(slice, e)
  342. }
  343. })
  344. slicev := reflect.New(typ).Elem()
  345. slicev.Set(reflect.ValueOf(slice))
  346. d.docType = docType
  347. return slicev
  348. }
  349. func (d *decoder) readRawDocElems(typ reflect.Type) reflect.Value {
  350. docType := d.docType
  351. d.docType = typ
  352. slice := make([]RawDocElem, 0, 8)
  353. d.readDocWith(func(kind byte, name string) {
  354. e := RawDocElem{Name: name}
  355. v := reflect.ValueOf(&e.Value)
  356. if d.readElemTo(v.Elem(), kind) {
  357. slice = append(slice, e)
  358. }
  359. })
  360. slicev := reflect.New(typ).Elem()
  361. slicev.Set(reflect.ValueOf(slice))
  362. d.docType = docType
  363. return slicev
  364. }
  365. func (d *decoder) readDocWith(f func(kind byte, name string)) {
  366. end := int(d.readInt32())
  367. end += d.i - 4
  368. if end <= d.i || end > len(d.in) || d.in[end-1] != '\x00' {
  369. corrupted()
  370. }
  371. for d.in[d.i] != '\x00' {
  372. kind := d.readByte()
  373. name := d.readCStr()
  374. if d.i >= end {
  375. corrupted()
  376. }
  377. f(kind, name)
  378. if d.i >= end {
  379. corrupted()
  380. }
  381. }
  382. d.i++ // '\x00'
  383. if d.i != end {
  384. corrupted()
  385. }
  386. }
  387. // --------------------------------------------------------------------------
  388. // Unmarshaling of individual elements within a document.
  389. var blackHole = settableValueOf(struct{}{})
  390. func (d *decoder) dropElem(kind byte) {
  391. d.readElemTo(blackHole, kind)
  392. }
  393. // Attempt to decode an element from the document and put it into out.
  394. // If the types are not compatible, the returned ok value will be
  395. // false and out will be unchanged.
  396. func (d *decoder) readElemTo(out reflect.Value, kind byte) (good bool) {
  397. start := d.i
  398. if kind == '\x03' {
  399. // Special case for documents. Delegate to readDocTo().
  400. switch out.Kind() {
  401. case reflect.Interface, reflect.Ptr, reflect.Struct, reflect.Map:
  402. d.readDocTo(out)
  403. default:
  404. switch out.Interface().(type) {
  405. case D:
  406. out.Set(d.readDocElems(out.Type()))
  407. case RawD:
  408. out.Set(d.readRawDocElems(out.Type()))
  409. default:
  410. d.readDocTo(blackHole)
  411. }
  412. }
  413. return true
  414. }
  415. var in interface{}
  416. switch kind {
  417. case 0x01: // Float64
  418. in = d.readFloat64()
  419. case 0x02: // UTF-8 string
  420. in = d.readStr()
  421. case 0x03: // Document
  422. panic("Can't happen. Handled above.")
  423. case 0x04: // Array
  424. outt := out.Type()
  425. for outt.Kind() == reflect.Ptr {
  426. outt = outt.Elem()
  427. }
  428. switch outt.Kind() {
  429. case reflect.Array:
  430. d.readArrayDocTo(out)
  431. return true
  432. case reflect.Slice:
  433. in = d.readSliceDoc(outt)
  434. default:
  435. in = d.readSliceDoc(typeSlice)
  436. }
  437. case 0x05: // Binary
  438. b := d.readBinary()
  439. if b.Kind == 0x00 || b.Kind == 0x02 {
  440. in = b.Data
  441. } else {
  442. in = b
  443. }
  444. case 0x06: // Undefined (obsolete, but still seen in the wild)
  445. in = Undefined
  446. case 0x07: // ObjectId
  447. in = ObjectId(d.readBytes(12))
  448. case 0x08: // Bool
  449. in = d.readBool()
  450. case 0x09: // Timestamp
  451. // MongoDB handles timestamps as milliseconds.
  452. i := d.readInt64()
  453. if i == -62135596800000 {
  454. in = time.Time{} // In UTC for convenience.
  455. } else {
  456. in = time.Unix(i/1e3, i%1e3*1e6)
  457. }
  458. case 0x0A: // Nil
  459. in = nil
  460. case 0x0B: // RegEx
  461. in = d.readRegEx()
  462. case 0x0D: // JavaScript without scope
  463. in = JavaScript{Code: d.readStr()}
  464. case 0x0E: // Symbol
  465. in = Symbol(d.readStr())
  466. case 0x0F: // JavaScript with scope
  467. d.i += 4 // Skip length
  468. js := JavaScript{d.readStr(), make(M)}
  469. d.readDocTo(reflect.ValueOf(js.Scope))
  470. in = js
  471. case 0x10: // Int32
  472. in = int(d.readInt32())
  473. case 0x11: // Mongo-specific timestamp
  474. in = MongoTimestamp(d.readInt64())
  475. case 0x12: // Int64
  476. in = d.readInt64()
  477. case 0x7F: // Max key
  478. in = MaxKey
  479. case 0xFF: // Min key
  480. in = MinKey
  481. default:
  482. panic(fmt.Sprintf("Unknown element kind (0x%02X)", kind))
  483. }
  484. outt := out.Type()
  485. if outt == typeRaw {
  486. out.Set(reflect.ValueOf(Raw{kind, d.in[start:d.i]}))
  487. return true
  488. }
  489. if setter := getSetter(outt, out); setter != nil {
  490. err := setter.SetBSON(Raw{kind, d.in[start:d.i]})
  491. if err == SetZero {
  492. out.Set(reflect.Zero(outt))
  493. return true
  494. }
  495. if err == nil {
  496. return true
  497. }
  498. if _, ok := err.(*TypeError); !ok {
  499. panic(err)
  500. }
  501. return false
  502. }
  503. if in == nil {
  504. out.Set(reflect.Zero(outt))
  505. return true
  506. }
  507. outk := outt.Kind()
  508. // Dereference and initialize pointer if necessary.
  509. first := true
  510. for outk == reflect.Ptr {
  511. if !out.IsNil() {
  512. out = out.Elem()
  513. } else {
  514. elem := reflect.New(outt.Elem())
  515. if first {
  516. // Only set if value is compatible.
  517. first = false
  518. defer func(out, elem reflect.Value) {
  519. if good {
  520. out.Set(elem)
  521. }
  522. }(out, elem)
  523. } else {
  524. out.Set(elem)
  525. }
  526. out = elem
  527. }
  528. outt = out.Type()
  529. outk = outt.Kind()
  530. }
  531. inv := reflect.ValueOf(in)
  532. if outt == inv.Type() {
  533. out.Set(inv)
  534. return true
  535. }
  536. switch outk {
  537. case reflect.Interface:
  538. out.Set(inv)
  539. return true
  540. case reflect.String:
  541. switch inv.Kind() {
  542. case reflect.String:
  543. out.SetString(inv.String())
  544. return true
  545. case reflect.Slice:
  546. if b, ok := in.([]byte); ok {
  547. out.SetString(string(b))
  548. return true
  549. }
  550. }
  551. case reflect.Slice, reflect.Array:
  552. // Remember, array (0x04) slices are built with the correct
  553. // element type. If we are here, must be a cross BSON kind
  554. // conversion (e.g. 0x05 unmarshalling on string).
  555. if outt.Elem().Kind() != reflect.Uint8 {
  556. break
  557. }
  558. switch inv.Kind() {
  559. case reflect.String:
  560. slice := []byte(inv.String())
  561. out.Set(reflect.ValueOf(slice))
  562. return true
  563. case reflect.Slice:
  564. switch outt.Kind() {
  565. case reflect.Array:
  566. reflect.Copy(out, inv)
  567. case reflect.Slice:
  568. out.SetBytes(inv.Bytes())
  569. }
  570. return true
  571. }
  572. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  573. switch inv.Kind() {
  574. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  575. out.SetInt(inv.Int())
  576. return true
  577. case reflect.Float32, reflect.Float64:
  578. out.SetInt(int64(inv.Float()))
  579. return true
  580. case reflect.Bool:
  581. if inv.Bool() {
  582. out.SetInt(1)
  583. } else {
  584. out.SetInt(0)
  585. }
  586. return true
  587. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  588. panic("Can't happen. No uint types in BSON?")
  589. }
  590. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  591. switch inv.Kind() {
  592. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  593. out.SetUint(uint64(inv.Int()))
  594. return true
  595. case reflect.Float32, reflect.Float64:
  596. out.SetUint(uint64(inv.Float()))
  597. return true
  598. case reflect.Bool:
  599. if inv.Bool() {
  600. out.SetUint(1)
  601. } else {
  602. out.SetUint(0)
  603. }
  604. return true
  605. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  606. panic("Can't happen. No uint types in BSON.")
  607. }
  608. case reflect.Float32, reflect.Float64:
  609. switch inv.Kind() {
  610. case reflect.Float32, reflect.Float64:
  611. out.SetFloat(inv.Float())
  612. return true
  613. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  614. out.SetFloat(float64(inv.Int()))
  615. return true
  616. case reflect.Bool:
  617. if inv.Bool() {
  618. out.SetFloat(1)
  619. } else {
  620. out.SetFloat(0)
  621. }
  622. return true
  623. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  624. panic("Can't happen. No uint types in BSON?")
  625. }
  626. case reflect.Bool:
  627. switch inv.Kind() {
  628. case reflect.Bool:
  629. out.SetBool(inv.Bool())
  630. return true
  631. case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
  632. out.SetBool(inv.Int() != 0)
  633. return true
  634. case reflect.Float32, reflect.Float64:
  635. out.SetBool(inv.Float() != 0)
  636. return true
  637. case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
  638. panic("Can't happen. No uint types in BSON?")
  639. }
  640. case reflect.Struct:
  641. if outt == typeURL && inv.Kind() == reflect.String {
  642. u, err := url.Parse(inv.String())
  643. if err != nil {
  644. panic(err)
  645. }
  646. out.Set(reflect.ValueOf(u).Elem())
  647. return true
  648. }
  649. }
  650. return false
  651. }
  652. // --------------------------------------------------------------------------
  653. // Parsers of basic types.
  654. func (d *decoder) readRegEx() RegEx {
  655. re := RegEx{}
  656. re.Pattern = d.readCStr()
  657. re.Options = d.readCStr()
  658. return re
  659. }
  660. func (d *decoder) readBinary() Binary {
  661. l := d.readInt32()
  662. b := Binary{}
  663. b.Kind = d.readByte()
  664. b.Data = d.readBytes(l)
  665. if b.Kind == 0x02 && len(b.Data) >= 4 {
  666. // Weird obsolete format with redundant length.
  667. b.Data = b.Data[4:]
  668. }
  669. return b
  670. }
  671. func (d *decoder) readStr() string {
  672. l := d.readInt32()
  673. b := d.readBytes(l - 1)
  674. if d.readByte() != '\x00' {
  675. corrupted()
  676. }
  677. return string(b)
  678. }
  679. func (d *decoder) readCStr() string {
  680. start := d.i
  681. end := start
  682. l := len(d.in)
  683. for ; end != l; end++ {
  684. if d.in[end] == '\x00' {
  685. break
  686. }
  687. }
  688. d.i = end + 1
  689. if d.i > l {
  690. corrupted()
  691. }
  692. return string(d.in[start:end])
  693. }
  694. func (d *decoder) readBool() bool {
  695. if d.readByte() == 1 {
  696. return true
  697. }
  698. return false
  699. }
  700. func (d *decoder) readFloat64() float64 {
  701. return math.Float64frombits(uint64(d.readInt64()))
  702. }
  703. func (d *decoder) readInt32() int32 {
  704. b := d.readBytes(4)
  705. return int32((uint32(b[0]) << 0) |
  706. (uint32(b[1]) << 8) |
  707. (uint32(b[2]) << 16) |
  708. (uint32(b[3]) << 24))
  709. }
  710. func (d *decoder) readInt64() int64 {
  711. b := d.readBytes(8)
  712. return int64((uint64(b[0]) << 0) |
  713. (uint64(b[1]) << 8) |
  714. (uint64(b[2]) << 16) |
  715. (uint64(b[3]) << 24) |
  716. (uint64(b[4]) << 32) |
  717. (uint64(b[5]) << 40) |
  718. (uint64(b[6]) << 48) |
  719. (uint64(b[7]) << 56))
  720. }
  721. func (d *decoder) readByte() byte {
  722. i := d.i
  723. d.i++
  724. if d.i > len(d.in) {
  725. corrupted()
  726. }
  727. return d.in[i]
  728. }
  729. func (d *decoder) readBytes(length int32) []byte {
  730. start := d.i
  731. d.i += int(length)
  732. if d.i > len(d.in) {
  733. corrupted()
  734. }
  735. return d.in[start : start+int(length)]
  736. }