Skip to content

Commit

Permalink
Add safe maskBytes
Browse files Browse the repository at this point in the history
Fixes #200.
  • Loading branch information
garyburd committed Jan 1, 2017
1 parent 5ddbd28 commit adf16b3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
6 changes: 6 additions & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"errors"
"io"
"io/ioutil"
"math/rand"
"net"
"strconv"
"sync"
Expand Down Expand Up @@ -180,6 +181,11 @@ var (
errInvalidControlFrame = errors.New("websocket: invalid control frame")
)

func newMaskKey() [4]byte {
n := rand.Uint32()
return [4]byte{byte(n), byte(n >> 8), byte(n >> 16), byte(n >> 24)}
}

func hideTempErr(err error) error {
if e, ok := err.(net.Error); ok && e.Temporary() {
err = &netError{msg: e.Error(), timeout: e.Timeout()}
Expand Down
12 changes: 3 additions & 9 deletions mask.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,14 @@
// this source code is governed by a BSD-style license that can be found in the
// LICENSE file.

// +build !appengine

package websocket

import (
"math/rand"
"unsafe"
)
import "unsafe"

const wordSize = int(unsafe.Sizeof(uintptr(0)))

func newMaskKey() [4]byte {
n := rand.Uint32()
return [4]byte{byte(n), byte(n >> 8), byte(n >> 16), byte(n >> 24)}
}

func maskBytes(key [4]byte, pos int, b []byte) int {

// Mask one byte at a time for small buffers.
Expand Down
15 changes: 15 additions & 0 deletions mask_safe.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright 2016 The Gorilla WebSocket Authors. All rights reserved. Use of
// this source code is governed by a BSD-style license that can be found in the
// LICENSE file.

// +build appengine

package websocket

func maskBytes(key [4]byte, pos int, b []byte) int {
for i := range b {
b[i] ^= key[pos&3]
pos++
}
return pos & 3
}
2 changes: 1 addition & 1 deletion mask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// LICENSE file.

// Require 1.7 for sub-bencmarks
// +build go1.7
// +build go1.7,!appengine

package websocket

Expand Down

0 comments on commit adf16b3

Please sign in to comment.