Copied from https://github.com/gorilla/websocket/blob/v1.5.0/mask.go
(key [4]byte, pos int, b []byte)
| 512 | |
| 513 | // Copied from https://github.com/gorilla/websocket/blob/v1.5.0/mask.go |
| 514 | func maskBytes(key [4]byte, pos int, b []byte) int { |
| 515 | // Mask one byte at a time for small buffers. |
| 516 | if len(b) < 2*wordSize { |
| 517 | for i := range b { |
| 518 | b[i] ^= key[pos&3] |
| 519 | pos++ |
| 520 | } |
| 521 | return pos & 3 |
| 522 | } |
| 523 | |
| 524 | // Mask one byte at a time to word boundary. |
| 525 | if n := int(uintptr(unsafe.Pointer(&b[0]))) % wordSize; n != 0 { |
| 526 | n = wordSize - n |
| 527 | for i := range b[:n] { |
| 528 | b[i] ^= key[pos&3] |
| 529 | pos++ |
| 530 | } |
| 531 | b = b[n:] |
| 532 | } |
| 533 | |
| 534 | // Create aligned word size key. |
| 535 | var k [wordSize]byte |
| 536 | for i := range k { |
| 537 | k[i] = key[(pos+i)&3] // nolint:gosec // false positive, impossible to be out of bounds; see: https://github.com/securego/gosec/issues/1525 |
| 538 | } |
| 539 | kw := *(*uintptr)(unsafe.Pointer(&k)) |
| 540 | |
| 541 | // Mask one word at a time. |
| 542 | n := (len(b) / wordSize) * wordSize |
| 543 | for i := 0; i < n; i += wordSize { |
| 544 | *(*uintptr)(unsafe.Add(unsafe.Pointer(&b[0]), i)) ^= kw |
| 545 | } |
| 546 | |
| 547 | // Mask one byte at a time for remaining bytes. |
| 548 | b = b[n:] |
| 549 | for i := range b { |
| 550 | b[i] ^= key[pos&3] |
| 551 | pos++ |
| 552 | } |
| 553 | |
| 554 | return pos & 3 |
| 555 | } |
| 556 | |
| 557 | // Copied from https://github.com/gorilla/websocket/blob/v1.5.0/conn.go#L184 |
| 558 | func newMaskKey() [4]byte { |
no test coverage detected