package wsutil

import (
	

	
	
)

// CipherReader implements io.Reader that applies xor-cipher to the bytes read
// from source.
// It could help to unmask WebSocket frame payload on the fly.
type CipherReader struct {
	r    io.Reader
	mask [4]byte
	pos  int
}

// NewCipherReader creates xor-cipher reader from r with given mask.
func ( io.Reader,  [4]byte) *CipherReader {
	return &CipherReader{, , 0}
}

// Reset resets CipherReader to read from r with given mask.
func ( *CipherReader) ( io.Reader,  [4]byte) {
	.r = 
	.mask = 
	.pos = 0
}

// Read implements io.Reader interface. It applies mask given during
// initialization to every read byte.
func ( *CipherReader) ( []byte) ( int,  error) {
	,  = .r.Read()
	ws.Cipher([:], .mask, .pos)
	.pos += 
	return , 
}

// CipherWriter implements io.Writer that applies xor-cipher to the bytes
// written to the destination writer. It does not modify the original bytes.
type CipherWriter struct {
	w    io.Writer
	mask [4]byte
	pos  int
}

// NewCipherWriter creates xor-cipher writer to w with given mask.
func ( io.Writer,  [4]byte) *CipherWriter {
	return &CipherWriter{, , 0}
}

// Reset reset CipherWriter to write to w with given mask.
func ( *CipherWriter) ( io.Writer,  [4]byte) {
	.w = 
	.mask = 
	.pos = 0
}

// Write implements io.Writer interface. It applies masking during
// initialization to every sent byte. It does not modify original slice.
func ( *CipherWriter) ( []byte) ( int,  error) {
	 := pbytes.GetLen(len())
	defer pbytes.Put()

	copy(, )
	ws.Cipher(, .mask, .pos)
	,  = .w.Write()
	.pos += 

	return , 
}