Browse Source

prepare to remove constructor of AuthenticationReader

pull/1524/head^2
Darien Raymond 6 years ago
parent
commit
7efa7ee632
No known key found for this signature in database
GPG Key ID: 7251FFA14BB18169
  1. 39
      common/crypto/auth.go

39
common/crypto/auth.go

@ -87,8 +87,10 @@ type AuthenticationReader struct {
sizeParser ChunkSizeDecoder sizeParser ChunkSizeDecoder
transferType protocol.TransferType transferType protocol.TransferType
padding PaddingLengthGenerator padding PaddingLengthGenerator
size int32 size uint16
paddingLen int32 paddingLen uint16
hasSize bool
done bool
} }
func NewAuthenticationReader(auth Authenticator, sizeParser ChunkSizeDecoder, reader io.Reader, transferType protocol.TransferType, paddingLen PaddingLengthGenerator) *AuthenticationReader { func NewAuthenticationReader(auth Authenticator, sizeParser ChunkSizeDecoder, reader io.Reader, transferType protocol.TransferType, paddingLen PaddingLengthGenerator) *AuthenticationReader {
@ -98,26 +100,24 @@ func NewAuthenticationReader(auth Authenticator, sizeParser ChunkSizeDecoder, re
sizeParser: sizeParser, sizeParser: sizeParser,
transferType: transferType, transferType: transferType,
padding: paddingLen, padding: paddingLen,
size: -1,
} }
} }
func (r *AuthenticationReader) readSize() (int32, int32, error) { func (r *AuthenticationReader) readSize() (uint16, uint16, error) {
if r.size != -1 { if r.hasSize {
s := r.size r.hasSize = false
r.size = -1 return r.size, r.paddingLen, nil
return s, r.paddingLen, nil
} }
sizeBytes := make([]byte, r.sizeParser.SizeBytes()) sizeBytes := make([]byte, r.sizeParser.SizeBytes())
if _, err := io.ReadFull(r.reader, sizeBytes); err != nil { if _, err := io.ReadFull(r.reader, sizeBytes); err != nil {
return 0, 0, err return 0, 0, err
} }
var padding int32 var padding uint16
if r.padding != nil { if r.padding != nil {
padding = int32(r.padding.NextPaddingLen()) padding = r.padding.NextPaddingLen()
} }
size, err := r.sizeParser.Decode(sizeBytes) size, err := r.sizeParser.Decode(sizeBytes)
return int32(size), padding, err return size, padding, err
} }
var errSoft = newError("waiting for more data") var errSoft = newError("waiting for more data")
@ -127,31 +127,36 @@ func (r *AuthenticationReader) readInternal(soft bool) (*buf.Buffer, error) {
return nil, errSoft return nil, errSoft
} }
if r.done {
return nil, io.EOF
}
size, padding, err := r.readSize() size, padding, err := r.readSize()
if err != nil { if err != nil {
return nil, err return nil, err
} }
if size == -2 || size == int32(r.auth.Overhead())+padding { if size == uint16(r.auth.Overhead())+padding {
r.size = -2 r.done = true
return nil, io.EOF return nil, io.EOF
} }
if soft && size > r.reader.BufferedBytes() { if soft && int32(size) > r.reader.BufferedBytes() {
r.size = size r.size = size
r.paddingLen = padding r.paddingLen = padding
r.hasSize = true
return nil, errSoft return nil, errSoft
} }
b := buf.NewSize(size) b := buf.NewSize(int32(size))
if err := b.Reset(buf.ReadFullFrom(r.reader, size)); err != nil { if err := b.Reset(buf.ReadFullFrom(r.reader, int32(size))); err != nil {
b.Release() b.Release()
return nil, err return nil, err
} }
size -= padding size -= padding
rb, err := r.auth.Open(b.BytesTo(0), b.BytesTo(size)) rb, err := r.auth.Open(b.BytesTo(0), b.BytesTo(int32(size)))
if err != nil { if err != nil {
b.Release() b.Release()
return nil, err return nil, err

Loading…
Cancel
Save