mirror of
https://github.com/jwetzell/osc-go.git
synced 2026-08-12 10:33:38 +00:00
fix up some OSC string parsing issues and test for them
This commit is contained in:
+8
-3
@@ -44,6 +44,11 @@ func MessageFromBytes(bytes []byte) (*OSCMessage, error) {
|
||||
Args: []OSCArg{},
|
||||
}
|
||||
|
||||
if len(typeAndArgBytes) == 0 {
|
||||
// NOTE(jwetzell): no type string return early.
|
||||
return &oscMessage, nil
|
||||
}
|
||||
|
||||
typeString, argBytes, err := readOSCString(typeAndArgBytes)
|
||||
|
||||
if err != nil {
|
||||
@@ -56,9 +61,9 @@ func MessageFromBytes(bytes []byte) (*OSCMessage, error) {
|
||||
return nil, errors.New("type string is malformed")
|
||||
}
|
||||
} else {
|
||||
oscArg, remainingBytes, error := readOSCArg(argBytes, string(oscType))
|
||||
if error != nil {
|
||||
return nil, error
|
||||
oscArg, remainingBytes, err := readOSCArg(argBytes, string(oscType))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
argBytes = remainingBytes
|
||||
oscMessage.Args = append(oscMessage.Args, oscArg)
|
||||
|
||||
+16
-2
@@ -298,18 +298,32 @@ func TestBadOSCMessageDecoding(t *testing.T) {
|
||||
{
|
||||
name: "address string not padded",
|
||||
bytes: []byte{47, 104, 101, 108, 108, 111, 0},
|
||||
errorString: "string data is not properly padded",
|
||||
errorString: "OSC string is not properly padded",
|
||||
},
|
||||
{
|
||||
name: "type string not padded",
|
||||
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 44, 0},
|
||||
errorString: "string data is not properly padded",
|
||||
errorString: "OSC string is not properly padded",
|
||||
},
|
||||
{
|
||||
name: "type string does not start with ,",
|
||||
bytes: []byte{47, 104, 101, 108, 108, 111, 0, 0, 45, 0, 0, 0},
|
||||
errorString: "type string is malformed",
|
||||
},
|
||||
{
|
||||
name: "string arg not null-terminated",
|
||||
bytes: []byte{
|
||||
47, 104, 101, 108, 108, 111, 0, 0, 44, 115, 0, 0, 97, 114, 103, 49,
|
||||
},
|
||||
errorString: "OSC string must be null-terminated",
|
||||
},
|
||||
{
|
||||
name: "string arg not padded",
|
||||
bytes: []byte{
|
||||
47, 104, 101, 108, 108, 111, 0, 0, 44, 115, 0, 0, 104, 105, 0,
|
||||
},
|
||||
errorString: "OSC string is not properly padded",
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
|
||||
@@ -172,18 +172,23 @@ func argsToBuffer(args []OSCArg) []byte {
|
||||
}
|
||||
|
||||
func readOSCString(bytes []byte) (string, []byte, error) {
|
||||
//TODO(jwetzell): add error handling
|
||||
oscString := ""
|
||||
stringEndIndex := 0
|
||||
|
||||
nullByteFound := false
|
||||
for index, byteIn := range bytes {
|
||||
if byteIn == 0 {
|
||||
nullByteFound = true
|
||||
oscString = string(bytes[0:index])
|
||||
stringEndIndex = index + 1
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !nullByteFound {
|
||||
return "", bytes, errors.New("OSC string must be null-terminated")
|
||||
}
|
||||
|
||||
stringPadding := 4 - (stringEndIndex % 4)
|
||||
|
||||
if stringPadding < 4 {
|
||||
@@ -191,7 +196,7 @@ func readOSCString(bytes []byte) (string, []byte, error) {
|
||||
}
|
||||
|
||||
if stringEndIndex > len(bytes) {
|
||||
return "", bytes, errors.New("string data is not properly padded")
|
||||
return "", bytes, errors.New("OSC string is not properly padded")
|
||||
}
|
||||
|
||||
remainingBytes := bytes[stringEndIndex:]
|
||||
|
||||
Reference in New Issue
Block a user