syncthing/protocol/marshal.go

143 lines
2.8 KiB
Go
Raw Normal View History

2013-12-15 10:43:31 +00:00
package protocol
import (
2013-12-21 22:52:20 +00:00
"errors"
2013-12-15 10:43:31 +00:00
"io"
2013-12-30 14:53:54 +00:00
"sync/atomic"
2013-12-15 10:43:31 +00:00
"github.com/calmh/syncthing/buffers"
)
func pad(l int) int {
d := l % 4
if d == 0 {
return 0
}
return 4 - d
}
var padBytes = []byte{0, 0, 0}
type marshalWriter struct {
w io.Writer
2013-12-30 14:53:54 +00:00
tot uint64
2013-12-15 10:43:31 +00:00
err error
2013-12-30 01:33:57 +00:00
b [8]byte
2013-12-15 10:43:31 +00:00
}
2013-12-21 22:52:20 +00:00
// We will never encode nor expect to decode blobs larger than 10 MB. Check
// inserted to protect against attempting to allocate arbitrary amounts of
// memory when reading a corrupt message.
const maxBytesFieldLength = 10 * 1 << 20
var ErrFieldLengthExceeded = errors.New("Raw bytes field size exceeds limit")
2013-12-15 10:43:31 +00:00
func (w *marshalWriter) writeString(s string) {
w.writeBytes([]byte(s))
}
func (w *marshalWriter) writeBytes(bs []byte) {
if w.err != nil {
return
}
2013-12-21 22:52:20 +00:00
if len(bs) > maxBytesFieldLength {
w.err = ErrFieldLengthExceeded
return
}
2013-12-15 10:43:31 +00:00
w.writeUint32(uint32(len(bs)))
if w.err != nil {
return
}
_, w.err = w.w.Write(bs)
2013-12-19 23:01:34 +00:00
if p := pad(len(bs)); w.err == nil && p > 0 {
_, w.err = w.w.Write(padBytes[:p])
2013-12-15 10:43:31 +00:00
}
2013-12-30 14:53:54 +00:00
atomic.AddUint64(&w.tot, uint64(len(bs)+pad(len(bs))))
2013-12-15 10:43:31 +00:00
}
func (w *marshalWriter) writeUint32(v uint32) {
if w.err != nil {
return
}
2013-12-30 01:33:57 +00:00
w.b[0] = byte(v >> 24)
w.b[1] = byte(v >> 16)
w.b[2] = byte(v >> 8)
w.b[3] = byte(v)
_, w.err = w.w.Write(w.b[:4])
2013-12-30 14:53:54 +00:00
atomic.AddUint64(&w.tot, 4)
2013-12-15 10:43:31 +00:00
}
func (w *marshalWriter) writeUint64(v uint64) {
if w.err != nil {
return
}
2013-12-30 01:33:57 +00:00
w.b[0] = byte(v >> 56)
w.b[1] = byte(v >> 48)
w.b[2] = byte(v >> 40)
w.b[3] = byte(v >> 32)
w.b[4] = byte(v >> 24)
w.b[5] = byte(v >> 16)
w.b[6] = byte(v >> 8)
w.b[7] = byte(v)
_, w.err = w.w.Write(w.b[:8])
2013-12-30 14:53:54 +00:00
atomic.AddUint64(&w.tot, 8)
}
func (w *marshalWriter) getTot() uint64 {
return atomic.LoadUint64(&w.tot)
2013-12-15 10:43:31 +00:00
}
type marshalReader struct {
r io.Reader
2013-12-30 14:53:54 +00:00
tot uint64
2013-12-15 10:43:31 +00:00
err error
2013-12-30 01:33:57 +00:00
b [8]byte
2013-12-15 10:43:31 +00:00
}
func (r *marshalReader) readString() string {
bs := r.readBytes()
defer buffers.Put(bs)
return string(bs)
}
func (r *marshalReader) readBytes() []byte {
if r.err != nil {
return nil
}
l := int(r.readUint32())
if r.err != nil {
return nil
}
2013-12-21 22:52:20 +00:00
if l > maxBytesFieldLength {
r.err = ErrFieldLengthExceeded
return nil
}
2013-12-15 10:43:31 +00:00
b := buffers.Get(l + pad(l))
_, r.err = io.ReadFull(r.r, b)
2013-12-30 14:53:54 +00:00
atomic.AddUint64(&r.tot, uint64(l+pad(l)))
2013-12-15 10:43:31 +00:00
return b[:l]
}
func (r *marshalReader) readUint32() uint32 {
if r.err != nil {
return 0
}
2013-12-30 01:33:57 +00:00
_, r.err = io.ReadFull(r.r, r.b[:4])
2013-12-30 14:53:54 +00:00
atomic.AddUint64(&r.tot, 8)
2013-12-30 01:33:57 +00:00
return uint32(r.b[3]) | uint32(r.b[2])<<8 | uint32(r.b[1])<<16 | uint32(r.b[0])<<24
2013-12-15 10:43:31 +00:00
}
func (r *marshalReader) readUint64() uint64 {
if r.err != nil {
return 0
}
2013-12-30 01:33:57 +00:00
_, r.err = io.ReadFull(r.r, r.b[:8])
2013-12-30 14:53:54 +00:00
atomic.AddUint64(&r.tot, 8)
2013-12-30 01:33:57 +00:00
return uint64(r.b[7]) | uint64(r.b[6])<<8 | uint64(r.b[5])<<16 | uint64(r.b[4])<<24 |
uint64(r.b[3])<<32 | uint64(r.b[2])<<40 | uint64(r.b[1])<<48 | uint64(r.b[0])<<56
2013-12-15 10:43:31 +00:00
}
2013-12-30 14:53:54 +00:00
func (r *marshalReader) getTot() uint64 {
return atomic.LoadUint64(&r.tot)
}