Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3b6e614

Browse files
committed
Rename xor to mask
1 parenta13f5dc commit3b6e614

File tree

4 files changed

+17
-16
lines changed

4 files changed

+17
-16
lines changed

‎conn.go‎

Lines changed: 5 additions & 5 deletions
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
ifh.masked {
328-
fastXOR(h.maskKey,b)
328+
mask(h.maskKey,b)
329329
}
330330

331331
switchh.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
ifh.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
ifc.client {
765-
err=binary.Read(rand.Reader,binary.BigEndian,&c.writeHeader.maskKey)
765+
err=binary.Read(rand.Reader,binary.LittleEndian,&c.writeHeader.maskKey)
766766
iferr!=nil {
767767
return0,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
returnn,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‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (c *Conn) ReadFrame(ctx context.Context) (OpCode, []byte, error) {
3737
return0,nil,err
3838
}
3939
ifh.masked {
40-
fastXOR(h.maskKey,b)
40+
mask(h.maskKey,b)
4141
}
4242
returnOpCode(h.opcode),b,nil
4343
}

‎frame.go‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ func (ce CloseError) bytes() ([]byte, error) {
322322
returnbuf,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-
funcfastXOR(keyuint32,b []byte)uint32 {
334+
//
335+
// See https://github.com/golang/go/issues/31586
336+
funcmask(keyuint32,b []byte)uint32 {
335337
iflen(b)>=8 {
336338
key64:=uint64(key)<<32|uint64(key)
337339

‎frame_test.go‎

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

311-
funcTest_xor(t*testing.T) {
311+
funcTest_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
ifexp:= []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-
funcbasixXOR(maskKey [4]byte,posint,b []byte)int {
328+
funcbasixMask(maskKey [4]byte,posint,b []byte)int {
329329
fori:=rangeb {
330330
b[i]^=maskKey[pos&3]
331331
pos++
332332
}
333333
returnpos&3
334334
}
335335

336-
funcBenchmarkXOR(b*testing.B) {
336+
funcBenchmark_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
fori:=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
fori:=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:=rangefns {
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)

[8]ページ先頭

©2009-2025 Movatter.jp