diff --git a/common/alloc/buffer.go b/common/alloc/buffer.go index 8a0183e5..ed5c2131 100644 --- a/common/alloc/buffer.go +++ b/common/alloc/buffer.go @@ -108,6 +108,7 @@ func (b *Buffer) Bytes() []byte { } func (b *Buffer) SetBytesFunc(writer BytesWriter) { + b.start = defaultOffset b.end = b.start + writer(b.v[b.start:]) } diff --git a/common/crypto/auth.go b/common/crypto/auth.go index b73ebfe9..5f911d90 100644 --- a/common/crypto/auth.go +++ b/common/crypto/auth.go @@ -121,38 +121,38 @@ func (v *AuthenticationReader) CopyChunk(b []byte) int { return nBytes } +func (v *AuthenticationReader) EnsureChunk() error { + for { + err := v.NextChunk() + if err == nil { + return nil + } + if err == errInsufficientBuffer { + if !v.buffer.IsEmpty() { + leftover := v.buffer.Bytes() + v.buffer.SetBytesFunc(func(b []byte) int { + return copy(b, leftover) + }) + } + _, err = v.buffer.FillFrom(v.reader) + } + return err + } +} + func (v *AuthenticationReader) Read(b []byte) (int, error) { if len(v.chunk) > 0 { nBytes := v.CopyChunk(b) return nBytes, nil } - totalBytes := 0 - for { - err := v.NextChunk() - if err == errInsufficientBuffer { - if totalBytes > 0 { - return totalBytes, nil - } - leftover := v.buffer.Bytes() - v.buffer.SetBytesFunc(func(b []byte) int { - return copy(b, leftover) - }) - _, err = v.buffer.FillFrom(v.reader) - } - - if err != nil { - return 0, err - } - - nBytes := v.CopyChunk(b) - b = b[nBytes:] - totalBytes += nBytes - - if !v.aggressive { - return totalBytes, nil - } + err := v.EnsureChunk() + if err != nil { + return 0, err } + + nBytes := v.CopyChunk(b) + return nBytes, nil } type AuthenticationWriter struct { diff --git a/common/io/reader_test.go b/common/io/reader_test.go index ff6eb0c8..f57f48e9 100644 --- a/common/io/reader_test.go +++ b/common/io/reader_test.go @@ -25,5 +25,5 @@ func TestAdaptiveReader(t *testing.T) { b2, err := reader.Read() assert.Error(err).IsNil() assert.Bool(b2.IsFull()).IsTrue() - assert.Int(buffer.Len()).Equals(778272) + assert.Int(buffer.Len()).Equals(1007648) }