mirror of
https://github.com/v2fly/v2ray-core.git
synced 2025-12-27 12:35:21 -05:00
optimize cfb stream writer
This commit is contained in:
@@ -3,6 +3,8 @@ package crypto
|
||||
import (
|
||||
"crypto/cipher"
|
||||
"io"
|
||||
|
||||
"v2ray.com/core/common/buf"
|
||||
)
|
||||
|
||||
type CryptionReader struct {
|
||||
@@ -17,10 +19,10 @@ func NewCryptionReader(stream cipher.Stream, reader io.Reader) *CryptionReader {
|
||||
}
|
||||
}
|
||||
|
||||
func (v *CryptionReader) Read(data []byte) (int, error) {
|
||||
nBytes, err := v.reader.Read(data)
|
||||
func (r *CryptionReader) Read(data []byte) (int, error) {
|
||||
nBytes, err := r.reader.Read(data)
|
||||
if nBytes > 0 {
|
||||
v.stream.XORKeyStream(data[:nBytes], data[:nBytes])
|
||||
r.stream.XORKeyStream(data[:nBytes], data[:nBytes])
|
||||
}
|
||||
return nBytes, err
|
||||
}
|
||||
@@ -39,7 +41,16 @@ func NewCryptionWriter(stream cipher.Stream, writer io.Writer) *CryptionWriter {
|
||||
}
|
||||
|
||||
// Write implements io.Writer.Write().
|
||||
func (v *CryptionWriter) Write(data []byte) (int, error) {
|
||||
v.stream.XORKeyStream(data, data)
|
||||
return v.writer.Write(data)
|
||||
func (w *CryptionWriter) Write(data []byte) (int, error) {
|
||||
w.stream.XORKeyStream(data, data)
|
||||
return w.writer.Write(data)
|
||||
}
|
||||
|
||||
func (w *CryptionWriter) WriteMultiBuffer(mb buf.MultiBuffer) (int, error) {
|
||||
bs := mb.ToNetBuffers()
|
||||
for _, b := range bs {
|
||||
w.stream.XORKeyStream(b, b)
|
||||
}
|
||||
nBytes, err := bs.WriteTo(w.writer)
|
||||
return int(nBytes), err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user