Skip to content

Commit 3b6e614

Browse files
committed
Rename xor to mask
1 parent a13f5dc commit 3b6e614

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

conn.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ func (c *Conn) handleControl(ctx context.Context, h header) error {
325325
}
326326

327327
if h.masked {
328-
fastXOR(h.maskKey, b)
328+
mask(h.maskKey, b)
329329
}
330330

331331
switch h.opcode {
@@ -446,8 +446,8 @@ func (c *Conn) reader(ctx context.Context, lock bool) (MessageType, io.Reader, e
446446

447447
c.readerMsgCtx = ctx
448448
c.readerMsgHeader = h
449-
c.readerMaskKey = h.maskKey
450449
c.readerFrameEOF = false
450+
c.readerMaskKey = h.maskKey
451451
c.readMsgLeft = c.msgReadLimit.Load()
452452

453453
r := &messageReader{
@@ -546,7 +546,7 @@ func (r *messageReader) read(p []byte, lock bool) (int, error) {
546546
h.payloadLength -= int64(n)
547547
r.c.readMsgLeft -= int64(n)
548548
if h.masked {
549-
r.c.readerMaskKey = fastXOR(r.c.readerMaskKey, p)
549+
r.c.readerMaskKey = mask(r.c.readerMaskKey, p)
550550
}
551551
r.c.readerMsgHeader = h
552552

@@ -762,7 +762,7 @@ func (c *Conn) writeFrame(ctx context.Context, fin bool, opcode opcode, p []byte
762762
c.writeHeader.payloadLength = int64(len(p))
763763

764764
if c.client {
765-
err = binary.Read(rand.Reader, binary.BigEndian, &c.writeHeader.maskKey)
765+
err = binary.Read(rand.Reader, binary.LittleEndian, &c.writeHeader.maskKey)
766766
if err != nil {
767767
return 0, fmt.Errorf("failed to generate masking key: %w", err)
768768
}
@@ -832,7 +832,7 @@ func (c *Conn) realWriteFrame(ctx context.Context, h header, p []byte) (n int, e
832832
return n, err
833833
}
834834

835-
maskKey = fastXOR(maskKey, c.writeBuf[i:i+n2])
835+
maskKey = mask(maskKey, c.writeBuf[i:i+n2])
836836

837837
p = p[n2:]
838838
n += n2

conn_export_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (c *Conn) ReadFrame(ctx context.Context) (OpCode, []byte, error) {
3737
return 0, nil, err
3838
}
3939
if h.masked {
40-
fastXOR(h.maskKey, b)
40+
mask(h.maskKey, b)
4141
}
4242
return OpCode(h.opcode), b, nil
4343
}

frame.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func (ce CloseError) bytes() ([]byte, error) {
322322
return buf, nil
323323
}
324324

325-
// fastXOR applies the WebSocket masking algorithm to p
325+
// fastMask applies the WebSocket masking algorithm to p
326326
// with the given key.
327327
// See https://tools.ietf.org/html/rfc6455#section-5.3
328328
//
@@ -331,7 +331,9 @@ func (ce CloseError) bytes() ([]byte, error) {
331331
//
332332
// It is optimized for LittleEndian and expects the key
333333
// to be in little endian.
334-
func fastXOR(key uint32, b []byte) uint32 {
334+
//
335+
// See https://github.com/golang/go/issues/31586
336+
func mask(key uint32, b []byte) uint32 {
335337
if len(b) >= 8 {
336338
key64 := uint64(key)<<32 | uint64(key)
337339

frame_test.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,13 @@ func Test_validWireCloseCode(t *testing.T) {
308308
}
309309
}
310310

311-
func Test_xor(t *testing.T) {
311+
func Test_mask(t *testing.T) {
312312
t.Parallel()
313313

314314
key := []byte{0xa, 0xb, 0xc, 0xff}
315315
key32 := binary.LittleEndian.Uint32(key)
316316
p := []byte{0xa, 0xb, 0xc, 0xf2, 0xc}
317-
gotKey32 := fastXOR(key32, p)
317+
gotKey32 := mask(key32, p)
318318

319319
if exp := []byte{0, 0, 0, 0x0d, 0x6}; !cmp.Equal(exp, p) {
320320
t.Fatalf("unexpected mask: %v", cmp.Diff(exp, p))
@@ -325,15 +325,15 @@ func Test_xor(t *testing.T) {
325325
}
326326
}
327327

328-
func basixXOR(maskKey [4]byte, pos int, b []byte) int {
328+
func basixMask(maskKey [4]byte, pos int, b []byte) int {
329329
for i := range b {
330330
b[i] ^= maskKey[pos&3]
331331
pos++
332332
}
333333
return pos & 3
334334
}
335335

336-
func BenchmarkXOR(b *testing.B) {
336+
func Benchmark_mask(b *testing.B) {
337337
sizes := []int{
338338
2,
339339
3,
@@ -355,18 +355,18 @@ func BenchmarkXOR(b *testing.B) {
355355
name: "basic",
356356
fn: func(b *testing.B, key [4]byte, p []byte) {
357357
for i := 0; i < b.N; i++ {
358-
basixXOR(key, 0, p)
358+
basixMask(key, 0, p)
359359
}
360360
},
361361
},
362362
{
363363
name: "fast",
364364
fn: func(b *testing.B, key [4]byte, p []byte) {
365-
key32 := binary.BigEndian.Uint32(key[:])
365+
key32 := binary.LittleEndian.Uint32(key[:])
366366
b.ResetTimer()
367367

368368
for i := 0; i < b.N; i++ {
369-
fastXOR(key32, p)
369+
mask(key32, p)
370370
}
371371
},
372372
},
@@ -384,7 +384,6 @@ func BenchmarkXOR(b *testing.B) {
384384
b.Run(strconv.Itoa(size), func(b *testing.B) {
385385
for _, fn := range fns {
386386
b.Run(fn.name, func(b *testing.B) {
387-
b.ReportAllocs()
388387
b.SetBytes(int64(size))
389388

390389
fn.fn(b, key, p)

0 commit comments

Comments
 (0)