Skip to content

Commit

Permalink
Merge 699e7b6 into 3cbc1ae
Browse files Browse the repository at this point in the history
  • Loading branch information
peternewman committed Mar 22, 2021
2 parents 3cbc1ae + 699e7b6 commit ad087dd
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
4 changes: 2 additions & 2 deletions osc/osc.go
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ func readPaddedString(reader *bufio.Reader) (string, int, error) {
str = str[:len(str)-1]

// Remove the padding bytes
padLen := padBytesNeeded(len(str)) - 1
padLen := padBytesNeeded(len(str))
if padLen > 0 {
n += padLen
padBytes := make([]byte, padLen)
Expand Down Expand Up @@ -1014,7 +1014,7 @@ func writePaddedString(str string, buf *bytes.Buffer) (int, error) {
// padBytesNeeded determines how many bytes are needed to fill up to the next 4
// byte length.
func padBytesNeeded(elementLen int) int {
return 4*(elementLen/4+1) - elementLen
return ((4 - (elementLen % 4)) % 4)
}

////
Expand Down
31 changes: 24 additions & 7 deletions osc/osc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,9 @@ func TestReadPaddedString(t *testing.T) {
s string // resulting string
}{
{[]byte{'t', 'e', 's', 't', 's', 't', 'r', 'i', 'n', 'g', 0, 0}, 12, "teststring"},
{[]byte{'t', 'e', 's', 't', 0, 0, 0, 0}, 8, "test"},
{[]byte{'t', 'e', 's', 't', 'e', 'r', 's', 0}, 8, "testers"},
{[]byte{'t', 'e', 's', 't', 's', 0, 0, 0}, 8, "tests"},
{[]byte{'t', 'e', 's', 't'}, 4, "test"},
} {
buf := bytes.NewBuffer(tt.buf)
s, n, err := readPaddedString(bufio.NewReader(buf))
Expand Down Expand Up @@ -391,28 +393,43 @@ func TestWritePaddedString(t *testing.T) {
func TestPadBytesNeeded(t *testing.T) {
var n int
n = padBytesNeeded(4)
if n != 4 {
t.Errorf("Number of pad bytes should be 4 and is: %d", n)
if n != 0 {
t.Errorf("Number of pad bytes should be 0 and is: %d", n)
}

n = padBytesNeeded(3)
if n != 1 {
t.Errorf("Number of pad bytes should be 1 and is: %d", n)
}

n = padBytesNeeded(2)
if n != 2 {
t.Errorf("Number of pad bytes should be 2 and is: %d", n)
}

n = padBytesNeeded(1)
if n != 3 {
t.Errorf("Number of pad bytes should be 3 and is: %d", n)
}

n = padBytesNeeded(0)
if n != 4 {
t.Errorf("Number of pad bytes should be 4 and is: %d", n)
if n != 0 {
t.Errorf("Number of pad bytes should be 0 and is: %d", n)
}

n = padBytesNeeded(5)
if n != 3 {
t.Errorf("Number of pad bytes should be 3 and is: %d", n)
}

n = padBytesNeeded(7)
if n != 1 {
t.Errorf("Number of pad bytes should be 1 and is: %d", n)
}

n = padBytesNeeded(32)
if n != 4 {
t.Errorf("Number of pad bytes should be 4 and is: %d", n)
if n != 0 {
t.Errorf("Number of pad bytes should be 0 and is: %d", n)
}

n = padBytesNeeded(63)
Expand Down

0 comments on commit ad087dd

Please sign in to comment.