本站源代码
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.

883 lines
20KB

  1. // Copyright (C) MongoDB, Inc. 2017-present.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License"); you may
  4. // not use this file except in compliance with the License. You may obtain
  5. // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
  6. package bsonrw
  7. import (
  8. "bytes"
  9. "encoding/binary"
  10. "errors"
  11. "fmt"
  12. "io"
  13. "math"
  14. "sync"
  15. "unicode"
  16. "go.mongodb.org/mongo-driver/bson/bsontype"
  17. "go.mongodb.org/mongo-driver/bson/primitive"
  18. )
  19. var _ ValueReader = (*valueReader)(nil)
  20. var vrPool = sync.Pool{
  21. New: func() interface{} {
  22. return new(valueReader)
  23. },
  24. }
  25. // BSONValueReaderPool is a pool for ValueReaders that read BSON.
  26. type BSONValueReaderPool struct {
  27. pool sync.Pool
  28. }
  29. // NewBSONValueReaderPool instantiates a new BSONValueReaderPool.
  30. func NewBSONValueReaderPool() *BSONValueReaderPool {
  31. return &BSONValueReaderPool{
  32. pool: sync.Pool{
  33. New: func() interface{} {
  34. return new(valueReader)
  35. },
  36. },
  37. }
  38. }
  39. // Get retrieves a ValueReader from the pool and uses src as the underlying BSON.
  40. func (bvrp *BSONValueReaderPool) Get(src []byte) ValueReader {
  41. vr := bvrp.pool.Get().(*valueReader)
  42. vr.reset(src)
  43. return vr
  44. }
  45. // Put inserts a ValueReader into the pool. If the ValueReader is not a BSON ValueReader nothing
  46. // is inserted into the pool and ok will be false.
  47. func (bvrp *BSONValueReaderPool) Put(vr ValueReader) (ok bool) {
  48. bvr, ok := vr.(*valueReader)
  49. if !ok {
  50. return false
  51. }
  52. bvr.reset(nil)
  53. bvrp.pool.Put(bvr)
  54. return true
  55. }
  56. // ErrEOA is the error returned when the end of a BSON array has been reached.
  57. var ErrEOA = errors.New("end of array")
  58. // ErrEOD is the error returned when the end of a BSON document has been reached.
  59. var ErrEOD = errors.New("end of document")
  60. type vrState struct {
  61. mode mode
  62. vType bsontype.Type
  63. end int64
  64. }
  65. // valueReader is for reading BSON values.
  66. type valueReader struct {
  67. offset int64
  68. d []byte
  69. stack []vrState
  70. frame int64
  71. }
  72. // NewBSONDocumentReader returns a ValueReader using b for the underlying BSON
  73. // representation. Parameter b must be a BSON Document.
  74. //
  75. // TODO(skriptble): There's a lack of symmetry between the reader and writer, since the reader takes
  76. // a []byte while the writer takes an io.Writer. We should have two versions of each, one that takes
  77. // a []byte and one that takes an io.Reader or io.Writer. The []byte version will need to return a
  78. // thing that can return the finished []byte since it might be reallocated when appended to.
  79. func NewBSONDocumentReader(b []byte) ValueReader {
  80. return newValueReader(b)
  81. }
  82. // NewBSONValueReader returns a ValueReader that starts in the Value mode instead of in top
  83. // level document mode. This enables the creation of a ValueReader for a single BSON value.
  84. func NewBSONValueReader(t bsontype.Type, val []byte) ValueReader {
  85. stack := make([]vrState, 1, 5)
  86. stack[0] = vrState{
  87. mode: mValue,
  88. vType: t,
  89. }
  90. return &valueReader{
  91. d: val,
  92. stack: stack,
  93. }
  94. }
  95. func newValueReader(b []byte) *valueReader {
  96. stack := make([]vrState, 1, 5)
  97. stack[0] = vrState{
  98. mode: mTopLevel,
  99. }
  100. return &valueReader{
  101. d: b,
  102. stack: stack,
  103. }
  104. }
  105. func (vr *valueReader) reset(b []byte) {
  106. if vr.stack == nil {
  107. vr.stack = make([]vrState, 1, 5)
  108. }
  109. vr.stack = vr.stack[:1]
  110. vr.stack[0] = vrState{mode: mTopLevel}
  111. vr.d = b
  112. vr.offset = 0
  113. vr.frame = 0
  114. }
  115. func (vr *valueReader) advanceFrame() {
  116. if vr.frame+1 >= int64(len(vr.stack)) { // We need to grow the stack
  117. length := len(vr.stack)
  118. if length+1 >= cap(vr.stack) {
  119. // double it
  120. buf := make([]vrState, 2*cap(vr.stack)+1)
  121. copy(buf, vr.stack)
  122. vr.stack = buf
  123. }
  124. vr.stack = vr.stack[:length+1]
  125. }
  126. vr.frame++
  127. // Clean the stack
  128. vr.stack[vr.frame].mode = 0
  129. vr.stack[vr.frame].vType = 0
  130. vr.stack[vr.frame].end = 0
  131. }
  132. func (vr *valueReader) pushDocument() error {
  133. vr.advanceFrame()
  134. vr.stack[vr.frame].mode = mDocument
  135. size, err := vr.readLength()
  136. if err != nil {
  137. return err
  138. }
  139. vr.stack[vr.frame].end = int64(size) + vr.offset - 4
  140. return nil
  141. }
  142. func (vr *valueReader) pushArray() error {
  143. vr.advanceFrame()
  144. vr.stack[vr.frame].mode = mArray
  145. size, err := vr.readLength()
  146. if err != nil {
  147. return err
  148. }
  149. vr.stack[vr.frame].end = int64(size) + vr.offset - 4
  150. return nil
  151. }
  152. func (vr *valueReader) pushElement(t bsontype.Type) {
  153. vr.advanceFrame()
  154. vr.stack[vr.frame].mode = mElement
  155. vr.stack[vr.frame].vType = t
  156. }
  157. func (vr *valueReader) pushValue(t bsontype.Type) {
  158. vr.advanceFrame()
  159. vr.stack[vr.frame].mode = mValue
  160. vr.stack[vr.frame].vType = t
  161. }
  162. func (vr *valueReader) pushCodeWithScope() (int64, error) {
  163. vr.advanceFrame()
  164. vr.stack[vr.frame].mode = mCodeWithScope
  165. size, err := vr.readLength()
  166. if err != nil {
  167. return 0, err
  168. }
  169. vr.stack[vr.frame].end = int64(size) + vr.offset - 4
  170. return int64(size), nil
  171. }
  172. func (vr *valueReader) pop() {
  173. switch vr.stack[vr.frame].mode {
  174. case mElement, mValue:
  175. vr.frame--
  176. case mDocument, mArray, mCodeWithScope:
  177. vr.frame -= 2 // we pop twice to jump over the vrElement: vrDocument -> vrElement -> vrDocument/TopLevel/etc...
  178. }
  179. }
  180. func (vr *valueReader) invalidTransitionErr(destination mode, name string, modes []mode) error {
  181. te := TransitionError{
  182. name: name,
  183. current: vr.stack[vr.frame].mode,
  184. destination: destination,
  185. modes: modes,
  186. action: "read",
  187. }
  188. if vr.frame != 0 {
  189. te.parent = vr.stack[vr.frame-1].mode
  190. }
  191. return te
  192. }
  193. func (vr *valueReader) typeError(t bsontype.Type) error {
  194. return fmt.Errorf("positioned on %s, but attempted to read %s", vr.stack[vr.frame].vType, t)
  195. }
  196. func (vr *valueReader) invalidDocumentLengthError() error {
  197. return fmt.Errorf("document is invalid, end byte is at %d, but null byte found at %d", vr.stack[vr.frame].end, vr.offset)
  198. }
  199. func (vr *valueReader) ensureElementValue(t bsontype.Type, destination mode, callerName string) error {
  200. switch vr.stack[vr.frame].mode {
  201. case mElement, mValue:
  202. if vr.stack[vr.frame].vType != t {
  203. return vr.typeError(t)
  204. }
  205. default:
  206. return vr.invalidTransitionErr(destination, callerName, []mode{mElement, mValue})
  207. }
  208. return nil
  209. }
  210. func (vr *valueReader) Type() bsontype.Type {
  211. return vr.stack[vr.frame].vType
  212. }
  213. func (vr *valueReader) nextElementLength() (int32, error) {
  214. var length int32
  215. var err error
  216. switch vr.stack[vr.frame].vType {
  217. case bsontype.Array, bsontype.EmbeddedDocument, bsontype.CodeWithScope:
  218. length, err = vr.peekLength()
  219. case bsontype.Binary:
  220. length, err = vr.peekLength()
  221. length += 4 + 1 // binary length + subtype byte
  222. case bsontype.Boolean:
  223. length = 1
  224. case bsontype.DBPointer:
  225. length, err = vr.peekLength()
  226. length += 4 + 12 // string length + ObjectID length
  227. case bsontype.DateTime, bsontype.Double, bsontype.Int64, bsontype.Timestamp:
  228. length = 8
  229. case bsontype.Decimal128:
  230. length = 16
  231. case bsontype.Int32:
  232. length = 4
  233. case bsontype.JavaScript, bsontype.String, bsontype.Symbol:
  234. length, err = vr.peekLength()
  235. length += 4
  236. case bsontype.MaxKey, bsontype.MinKey, bsontype.Null, bsontype.Undefined:
  237. length = 0
  238. case bsontype.ObjectID:
  239. length = 12
  240. case bsontype.Regex:
  241. regex := bytes.IndexByte(vr.d[vr.offset:], 0x00)
  242. if regex < 0 {
  243. err = io.EOF
  244. break
  245. }
  246. pattern := bytes.IndexByte(vr.d[vr.offset+int64(regex)+1:], 0x00)
  247. if pattern < 0 {
  248. err = io.EOF
  249. break
  250. }
  251. length = int32(int64(regex) + 1 + int64(pattern) + 1)
  252. default:
  253. return 0, fmt.Errorf("attempted to read bytes of unknown BSON type %v", vr.stack[vr.frame].vType)
  254. }
  255. return length, err
  256. }
  257. func (vr *valueReader) ReadValueBytes(dst []byte) (bsontype.Type, []byte, error) {
  258. switch vr.stack[vr.frame].mode {
  259. case mTopLevel:
  260. length, err := vr.peekLength()
  261. if err != nil {
  262. return bsontype.Type(0), nil, err
  263. }
  264. dst, err = vr.appendBytes(dst, length)
  265. if err != nil {
  266. return bsontype.Type(0), nil, err
  267. }
  268. return bsontype.Type(0), dst, nil
  269. case mElement, mValue:
  270. length, err := vr.nextElementLength()
  271. if err != nil {
  272. return bsontype.Type(0), dst, err
  273. }
  274. dst, err = vr.appendBytes(dst, length)
  275. t := vr.stack[vr.frame].vType
  276. vr.pop()
  277. return t, dst, err
  278. default:
  279. return bsontype.Type(0), nil, vr.invalidTransitionErr(0, "ReadValueBytes", []mode{mElement, mValue})
  280. }
  281. }
  282. func (vr *valueReader) Skip() error {
  283. switch vr.stack[vr.frame].mode {
  284. case mElement, mValue:
  285. default:
  286. return vr.invalidTransitionErr(0, "Skip", []mode{mElement, mValue})
  287. }
  288. length, err := vr.nextElementLength()
  289. if err != nil {
  290. return err
  291. }
  292. err = vr.skipBytes(length)
  293. vr.pop()
  294. return err
  295. }
  296. func (vr *valueReader) ReadArray() (ArrayReader, error) {
  297. if err := vr.ensureElementValue(bsontype.Array, mArray, "ReadArray"); err != nil {
  298. return nil, err
  299. }
  300. err := vr.pushArray()
  301. if err != nil {
  302. return nil, err
  303. }
  304. return vr, nil
  305. }
  306. func (vr *valueReader) ReadBinary() (b []byte, btype byte, err error) {
  307. if err := vr.ensureElementValue(bsontype.Binary, 0, "ReadBinary"); err != nil {
  308. return nil, 0, err
  309. }
  310. length, err := vr.readLength()
  311. if err != nil {
  312. return nil, 0, err
  313. }
  314. btype, err = vr.readByte()
  315. if err != nil {
  316. return nil, 0, err
  317. }
  318. if btype == 0x02 {
  319. length, err = vr.readLength()
  320. if err != nil {
  321. return nil, 0, err
  322. }
  323. }
  324. b, err = vr.readBytes(length)
  325. if err != nil {
  326. return nil, 0, err
  327. }
  328. vr.pop()
  329. return b, btype, nil
  330. }
  331. func (vr *valueReader) ReadBoolean() (bool, error) {
  332. if err := vr.ensureElementValue(bsontype.Boolean, 0, "ReadBoolean"); err != nil {
  333. return false, err
  334. }
  335. b, err := vr.readByte()
  336. if err != nil {
  337. return false, err
  338. }
  339. if b > 1 {
  340. return false, fmt.Errorf("invalid byte for boolean, %b", b)
  341. }
  342. vr.pop()
  343. return b == 1, nil
  344. }
  345. func (vr *valueReader) ReadDocument() (DocumentReader, error) {
  346. switch vr.stack[vr.frame].mode {
  347. case mTopLevel:
  348. // read size
  349. size, err := vr.readLength()
  350. if err != nil {
  351. return nil, err
  352. }
  353. if int(size) != len(vr.d) {
  354. return nil, fmt.Errorf("invalid document length")
  355. }
  356. vr.stack[vr.frame].end = int64(size) + vr.offset - 4
  357. return vr, nil
  358. case mElement, mValue:
  359. if vr.stack[vr.frame].vType != bsontype.EmbeddedDocument {
  360. return nil, vr.typeError(bsontype.EmbeddedDocument)
  361. }
  362. default:
  363. return nil, vr.invalidTransitionErr(mDocument, "ReadDocument", []mode{mTopLevel, mElement, mValue})
  364. }
  365. err := vr.pushDocument()
  366. if err != nil {
  367. return nil, err
  368. }
  369. return vr, nil
  370. }
  371. func (vr *valueReader) ReadCodeWithScope() (code string, dr DocumentReader, err error) {
  372. if err := vr.ensureElementValue(bsontype.CodeWithScope, 0, "ReadCodeWithScope"); err != nil {
  373. return "", nil, err
  374. }
  375. totalLength, err := vr.readLength()
  376. if err != nil {
  377. return "", nil, err
  378. }
  379. strLength, err := vr.readLength()
  380. if err != nil {
  381. return "", nil, err
  382. }
  383. strBytes, err := vr.readBytes(strLength)
  384. if err != nil {
  385. return "", nil, err
  386. }
  387. code = string(strBytes[:len(strBytes)-1])
  388. size, err := vr.pushCodeWithScope()
  389. if err != nil {
  390. return "", nil, err
  391. }
  392. // The total length should equal:
  393. // 4 (total length) + strLength + 4 (the length of str itself) + (document length)
  394. componentsLength := int64(4+strLength+4) + size
  395. if int64(totalLength) != componentsLength {
  396. return "", nil, fmt.Errorf(
  397. "length of CodeWithScope does not match lengths of components; total: %d; components: %d",
  398. totalLength, componentsLength,
  399. )
  400. }
  401. return code, vr, nil
  402. }
  403. func (vr *valueReader) ReadDBPointer() (ns string, oid primitive.ObjectID, err error) {
  404. if err := vr.ensureElementValue(bsontype.DBPointer, 0, "ReadDBPointer"); err != nil {
  405. return "", oid, err
  406. }
  407. ns, err = vr.readString()
  408. if err != nil {
  409. return "", oid, err
  410. }
  411. oidbytes, err := vr.readBytes(12)
  412. if err != nil {
  413. return "", oid, err
  414. }
  415. copy(oid[:], oidbytes)
  416. vr.pop()
  417. return ns, oid, nil
  418. }
  419. func (vr *valueReader) ReadDateTime() (int64, error) {
  420. if err := vr.ensureElementValue(bsontype.DateTime, 0, "ReadDateTime"); err != nil {
  421. return 0, err
  422. }
  423. i, err := vr.readi64()
  424. if err != nil {
  425. return 0, err
  426. }
  427. vr.pop()
  428. return i, nil
  429. }
  430. func (vr *valueReader) ReadDecimal128() (primitive.Decimal128, error) {
  431. if err := vr.ensureElementValue(bsontype.Decimal128, 0, "ReadDecimal128"); err != nil {
  432. return primitive.Decimal128{}, err
  433. }
  434. b, err := vr.readBytes(16)
  435. if err != nil {
  436. return primitive.Decimal128{}, err
  437. }
  438. l := binary.LittleEndian.Uint64(b[0:8])
  439. h := binary.LittleEndian.Uint64(b[8:16])
  440. vr.pop()
  441. return primitive.NewDecimal128(h, l), nil
  442. }
  443. func (vr *valueReader) ReadDouble() (float64, error) {
  444. if err := vr.ensureElementValue(bsontype.Double, 0, "ReadDouble"); err != nil {
  445. return 0, err
  446. }
  447. u, err := vr.readu64()
  448. if err != nil {
  449. return 0, err
  450. }
  451. vr.pop()
  452. return math.Float64frombits(u), nil
  453. }
  454. func (vr *valueReader) ReadInt32() (int32, error) {
  455. if err := vr.ensureElementValue(bsontype.Int32, 0, "ReadInt32"); err != nil {
  456. return 0, err
  457. }
  458. vr.pop()
  459. return vr.readi32()
  460. }
  461. func (vr *valueReader) ReadInt64() (int64, error) {
  462. if err := vr.ensureElementValue(bsontype.Int64, 0, "ReadInt64"); err != nil {
  463. return 0, err
  464. }
  465. vr.pop()
  466. return vr.readi64()
  467. }
  468. func (vr *valueReader) ReadJavascript() (code string, err error) {
  469. if err := vr.ensureElementValue(bsontype.JavaScript, 0, "ReadJavascript"); err != nil {
  470. return "", err
  471. }
  472. vr.pop()
  473. return vr.readString()
  474. }
  475. func (vr *valueReader) ReadMaxKey() error {
  476. if err := vr.ensureElementValue(bsontype.MaxKey, 0, "ReadMaxKey"); err != nil {
  477. return err
  478. }
  479. vr.pop()
  480. return nil
  481. }
  482. func (vr *valueReader) ReadMinKey() error {
  483. if err := vr.ensureElementValue(bsontype.MinKey, 0, "ReadMinKey"); err != nil {
  484. return err
  485. }
  486. vr.pop()
  487. return nil
  488. }
  489. func (vr *valueReader) ReadNull() error {
  490. if err := vr.ensureElementValue(bsontype.Null, 0, "ReadNull"); err != nil {
  491. return err
  492. }
  493. vr.pop()
  494. return nil
  495. }
  496. func (vr *valueReader) ReadObjectID() (primitive.ObjectID, error) {
  497. if err := vr.ensureElementValue(bsontype.ObjectID, 0, "ReadObjectID"); err != nil {
  498. return primitive.ObjectID{}, err
  499. }
  500. oidbytes, err := vr.readBytes(12)
  501. if err != nil {
  502. return primitive.ObjectID{}, err
  503. }
  504. var oid primitive.ObjectID
  505. copy(oid[:], oidbytes)
  506. vr.pop()
  507. return oid, nil
  508. }
  509. func (vr *valueReader) ReadRegex() (string, string, error) {
  510. if err := vr.ensureElementValue(bsontype.Regex, 0, "ReadRegex"); err != nil {
  511. return "", "", err
  512. }
  513. pattern, err := vr.readCString()
  514. if err != nil {
  515. return "", "", err
  516. }
  517. options, err := vr.readCString()
  518. if err != nil {
  519. return "", "", err
  520. }
  521. vr.pop()
  522. return pattern, options, nil
  523. }
  524. func (vr *valueReader) ReadString() (string, error) {
  525. if err := vr.ensureElementValue(bsontype.String, 0, "ReadString"); err != nil {
  526. return "", err
  527. }
  528. vr.pop()
  529. return vr.readString()
  530. }
  531. func (vr *valueReader) ReadSymbol() (symbol string, err error) {
  532. if err := vr.ensureElementValue(bsontype.Symbol, 0, "ReadSymbol"); err != nil {
  533. return "", err
  534. }
  535. vr.pop()
  536. return vr.readString()
  537. }
  538. func (vr *valueReader) ReadTimestamp() (t uint32, i uint32, err error) {
  539. if err := vr.ensureElementValue(bsontype.Timestamp, 0, "ReadTimestamp"); err != nil {
  540. return 0, 0, err
  541. }
  542. i, err = vr.readu32()
  543. if err != nil {
  544. return 0, 0, err
  545. }
  546. t, err = vr.readu32()
  547. if err != nil {
  548. return 0, 0, err
  549. }
  550. vr.pop()
  551. return t, i, nil
  552. }
  553. func (vr *valueReader) ReadUndefined() error {
  554. if err := vr.ensureElementValue(bsontype.Undefined, 0, "ReadUndefined"); err != nil {
  555. return err
  556. }
  557. vr.pop()
  558. return nil
  559. }
  560. func (vr *valueReader) ReadElement() (string, ValueReader, error) {
  561. switch vr.stack[vr.frame].mode {
  562. case mTopLevel, mDocument, mCodeWithScope:
  563. default:
  564. return "", nil, vr.invalidTransitionErr(mElement, "ReadElement", []mode{mTopLevel, mDocument, mCodeWithScope})
  565. }
  566. t, err := vr.readByte()
  567. if err != nil {
  568. return "", nil, err
  569. }
  570. if t == 0 {
  571. if vr.offset != vr.stack[vr.frame].end {
  572. return "", nil, vr.invalidDocumentLengthError()
  573. }
  574. vr.pop()
  575. return "", nil, ErrEOD
  576. }
  577. name, err := vr.readCString()
  578. if err != nil {
  579. return "", nil, err
  580. }
  581. vr.pushElement(bsontype.Type(t))
  582. return name, vr, nil
  583. }
  584. func (vr *valueReader) ReadValue() (ValueReader, error) {
  585. switch vr.stack[vr.frame].mode {
  586. case mArray:
  587. default:
  588. return nil, vr.invalidTransitionErr(mValue, "ReadValue", []mode{mArray})
  589. }
  590. t, err := vr.readByte()
  591. if err != nil {
  592. return nil, err
  593. }
  594. if t == 0 {
  595. if vr.offset != vr.stack[vr.frame].end {
  596. return nil, vr.invalidDocumentLengthError()
  597. }
  598. vr.pop()
  599. return nil, ErrEOA
  600. }
  601. _, err = vr.readCString()
  602. if err != nil {
  603. return nil, err
  604. }
  605. vr.pushValue(bsontype.Type(t))
  606. return vr, nil
  607. }
  608. func (vr *valueReader) readBytes(length int32) ([]byte, error) {
  609. if length < 0 {
  610. return nil, fmt.Errorf("invalid length: %d", length)
  611. }
  612. if vr.offset+int64(length) > int64(len(vr.d)) {
  613. return nil, io.EOF
  614. }
  615. start := vr.offset
  616. vr.offset += int64(length)
  617. return vr.d[start : start+int64(length)], nil
  618. }
  619. func (vr *valueReader) appendBytes(dst []byte, length int32) ([]byte, error) {
  620. if vr.offset+int64(length) > int64(len(vr.d)) {
  621. return nil, io.EOF
  622. }
  623. start := vr.offset
  624. vr.offset += int64(length)
  625. return append(dst, vr.d[start:start+int64(length)]...), nil
  626. }
  627. func (vr *valueReader) skipBytes(length int32) error {
  628. if vr.offset+int64(length) > int64(len(vr.d)) {
  629. return io.EOF
  630. }
  631. vr.offset += int64(length)
  632. return nil
  633. }
  634. func (vr *valueReader) readByte() (byte, error) {
  635. if vr.offset+1 > int64(len(vr.d)) {
  636. return 0x0, io.EOF
  637. }
  638. vr.offset++
  639. return vr.d[vr.offset-1], nil
  640. }
  641. func (vr *valueReader) readCString() (string, error) {
  642. idx := bytes.IndexByte(vr.d[vr.offset:], 0x00)
  643. if idx < 0 {
  644. return "", io.EOF
  645. }
  646. start := vr.offset
  647. // idx does not include the null byte
  648. vr.offset += int64(idx) + 1
  649. return string(vr.d[start : start+int64(idx)]), nil
  650. }
  651. func (vr *valueReader) skipCString() error {
  652. idx := bytes.IndexByte(vr.d[vr.offset:], 0x00)
  653. if idx < 0 {
  654. return io.EOF
  655. }
  656. // idx does not include the null byte
  657. vr.offset += int64(idx) + 1
  658. return nil
  659. }
  660. func (vr *valueReader) readString() (string, error) {
  661. length, err := vr.readLength()
  662. if err != nil {
  663. return "", err
  664. }
  665. if int64(length)+vr.offset > int64(len(vr.d)) {
  666. return "", io.EOF
  667. }
  668. if length <= 0 {
  669. return "", fmt.Errorf("invalid string length: %d", length)
  670. }
  671. if vr.d[vr.offset+int64(length)-1] != 0x00 {
  672. return "", fmt.Errorf("string does not end with null byte, but with %v", vr.d[vr.offset+int64(length)-1])
  673. }
  674. start := vr.offset
  675. vr.offset += int64(length)
  676. if length == 2 {
  677. asciiByte := vr.d[start]
  678. if asciiByte > unicode.MaxASCII {
  679. return "", fmt.Errorf("invalid ascii byte")
  680. }
  681. }
  682. return string(vr.d[start : start+int64(length)-1]), nil
  683. }
  684. func (vr *valueReader) peekLength() (int32, error) {
  685. if vr.offset+4 > int64(len(vr.d)) {
  686. return 0, io.EOF
  687. }
  688. idx := vr.offset
  689. return (int32(vr.d[idx]) | int32(vr.d[idx+1])<<8 | int32(vr.d[idx+2])<<16 | int32(vr.d[idx+3])<<24), nil
  690. }
  691. func (vr *valueReader) readLength() (int32, error) { return vr.readi32() }
  692. func (vr *valueReader) readi32() (int32, error) {
  693. if vr.offset+4 > int64(len(vr.d)) {
  694. return 0, io.EOF
  695. }
  696. idx := vr.offset
  697. vr.offset += 4
  698. return (int32(vr.d[idx]) | int32(vr.d[idx+1])<<8 | int32(vr.d[idx+2])<<16 | int32(vr.d[idx+3])<<24), nil
  699. }
  700. func (vr *valueReader) readu32() (uint32, error) {
  701. if vr.offset+4 > int64(len(vr.d)) {
  702. return 0, io.EOF
  703. }
  704. idx := vr.offset
  705. vr.offset += 4
  706. return (uint32(vr.d[idx]) | uint32(vr.d[idx+1])<<8 | uint32(vr.d[idx+2])<<16 | uint32(vr.d[idx+3])<<24), nil
  707. }
  708. func (vr *valueReader) readi64() (int64, error) {
  709. if vr.offset+8 > int64(len(vr.d)) {
  710. return 0, io.EOF
  711. }
  712. idx := vr.offset
  713. vr.offset += 8
  714. return int64(vr.d[idx]) | int64(vr.d[idx+1])<<8 | int64(vr.d[idx+2])<<16 | int64(vr.d[idx+3])<<24 |
  715. int64(vr.d[idx+4])<<32 | int64(vr.d[idx+5])<<40 | int64(vr.d[idx+6])<<48 | int64(vr.d[idx+7])<<56, nil
  716. }
  717. func (vr *valueReader) readu64() (uint64, error) {
  718. if vr.offset+8 > int64(len(vr.d)) {
  719. return 0, io.EOF
  720. }
  721. idx := vr.offset
  722. vr.offset += 8
  723. return uint64(vr.d[idx]) | uint64(vr.d[idx+1])<<8 | uint64(vr.d[idx+2])<<16 | uint64(vr.d[idx+3])<<24 |
  724. uint64(vr.d[idx+4])<<32 | uint64(vr.d[idx+5])<<40 | uint64(vr.d[idx+6])<<48 | uint64(vr.d[idx+7])<<56, nil
  725. }
上海开阖软件有限公司 沪ICP备12045867号-1