(oscNum string, barr []byte)
| 72 | } |
| 73 | |
| 74 | func EncodeWaveOSCBytes(oscNum string, barr []byte) ([]byte, error) { |
| 75 | if len(oscNum) != 5 { |
| 76 | return nil, fmt.Errorf("oscNum must be 5 characters") |
| 77 | } |
| 78 | const maxSize = 64 * 1024 * 1024 // 64 MB |
| 79 | if len(barr) > maxSize { |
| 80 | return nil, fmt.Errorf("input data too large") |
| 81 | } |
| 82 | hasControlChars := false |
| 83 | for _, b := range barr { |
| 84 | if b < 0x20 || b == 0x7F { |
| 85 | hasControlChars = true |
| 86 | break |
| 87 | } |
| 88 | } |
| 89 | if !hasControlChars { |
| 90 | // If no control characters, directly construct the output |
| 91 | // \x1b] (2) + WaveOSC + ; (1) + message + \x07 (1) |
| 92 | output := make([]byte, oscPrefixLen(oscNum)+len(barr)+1) |
| 93 | copyOscPrefix(output, oscNum) |
| 94 | copy(output[oscPrefixLen(oscNum):], barr) |
| 95 | output[len(output)-1] = BEL |
| 96 | return output, nil |
| 97 | } |
| 98 | |
| 99 | var buf bytes.Buffer |
| 100 | buf.Write(makeOscPrefix(oscNum)) |
| 101 | escSeq := [6]byte{'\\', 'u', '0', '0', '0', '0'} |
| 102 | for _, b := range barr { |
| 103 | if b < 0x20 || b == 0x7f { |
| 104 | escSeq[4] = HexChars[b>>4] |
| 105 | escSeq[5] = HexChars[b&0x0f] |
| 106 | buf.Write(escSeq[:]) |
| 107 | } else { |
| 108 | buf.WriteByte(b) |
| 109 | } |
| 110 | } |
| 111 | buf.WriteByte(BEL) |
| 112 | return buf.Bytes(), nil |
| 113 | } |
| 114 | |
| 115 | func EncodeWaveOSCMessageEx(oscNum string, msg *RpcMessage) ([]byte, error) { |
| 116 | if msg == nil { |
no test coverage detected