[822] | 1 | package pq
|
---|
| 2 |
|
---|
| 3 | import (
|
---|
| 4 | "bytes"
|
---|
| 5 | "encoding/binary"
|
---|
| 6 |
|
---|
| 7 | "github.com/lib/pq/oid"
|
---|
| 8 | )
|
---|
| 9 |
|
---|
| 10 | type readBuf []byte
|
---|
| 11 |
|
---|
| 12 | func (b *readBuf) int32() (n int) {
|
---|
| 13 | n = int(int32(binary.BigEndian.Uint32(*b)))
|
---|
| 14 | *b = (*b)[4:]
|
---|
| 15 | return
|
---|
| 16 | }
|
---|
| 17 |
|
---|
| 18 | func (b *readBuf) oid() (n oid.Oid) {
|
---|
| 19 | n = oid.Oid(binary.BigEndian.Uint32(*b))
|
---|
| 20 | *b = (*b)[4:]
|
---|
| 21 | return
|
---|
| 22 | }
|
---|
| 23 |
|
---|
| 24 | // N.B: this is actually an unsigned 16-bit integer, unlike int32
|
---|
| 25 | func (b *readBuf) int16() (n int) {
|
---|
| 26 | n = int(binary.BigEndian.Uint16(*b))
|
---|
| 27 | *b = (*b)[2:]
|
---|
| 28 | return
|
---|
| 29 | }
|
---|
| 30 |
|
---|
| 31 | func (b *readBuf) string() string {
|
---|
| 32 | i := bytes.IndexByte(*b, 0)
|
---|
| 33 | if i < 0 {
|
---|
| 34 | errorf("invalid message format; expected string terminator")
|
---|
| 35 | }
|
---|
| 36 | s := (*b)[:i]
|
---|
| 37 | *b = (*b)[i+1:]
|
---|
| 38 | return string(s)
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | func (b *readBuf) next(n int) (v []byte) {
|
---|
| 42 | v = (*b)[:n]
|
---|
| 43 | *b = (*b)[n:]
|
---|
| 44 | return
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | func (b *readBuf) byte() byte {
|
---|
| 48 | return b.next(1)[0]
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | type writeBuf struct {
|
---|
| 52 | buf []byte
|
---|
| 53 | pos int
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | func (b *writeBuf) int32(n int) {
|
---|
| 57 | x := make([]byte, 4)
|
---|
| 58 | binary.BigEndian.PutUint32(x, uint32(n))
|
---|
| 59 | b.buf = append(b.buf, x...)
|
---|
| 60 | }
|
---|
| 61 |
|
---|
| 62 | func (b *writeBuf) int16(n int) {
|
---|
| 63 | x := make([]byte, 2)
|
---|
| 64 | binary.BigEndian.PutUint16(x, uint16(n))
|
---|
| 65 | b.buf = append(b.buf, x...)
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | func (b *writeBuf) string(s string) {
|
---|
| 69 | b.buf = append(append(b.buf, s...), '\000')
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 | func (b *writeBuf) byte(c byte) {
|
---|
| 73 | b.buf = append(b.buf, c)
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | func (b *writeBuf) bytes(v []byte) {
|
---|
| 77 | b.buf = append(b.buf, v...)
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | func (b *writeBuf) wrap() []byte {
|
---|
| 81 | p := b.buf[b.pos:]
|
---|
| 82 | binary.BigEndian.PutUint32(p, uint32(len(p)))
|
---|
| 83 | return b.buf
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | func (b *writeBuf) next(c byte) {
|
---|
| 87 | p := b.buf[b.pos:]
|
---|
| 88 | binary.BigEndian.PutUint32(p, uint32(len(p)))
|
---|
| 89 | b.pos = len(b.buf) + 1
|
---|
| 90 | b.buf = append(b.buf, c, 0, 0, 0, 0)
|
---|
| 91 | }
|
---|