summaryrefslogtreecommitdiff
path: root/libs/evoral/evoral/midi_util.h
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2009-02-16 00:36:11 +0000
committerDavid Robillard <d@drobilla.net>2009-02-16 00:36:11 +0000
commit85ab34179583db06899105586e7ac2d264b437fb (patch)
tree2ce8b180e67a369d9deb0fac196f660a375d65f4 /libs/evoral/evoral/midi_util.h
parent473170200d3ce2a96b1425b6dfd57003124d8276 (diff)
Gracefully ignore illegal MIDI events.
git-svn-id: svn://localhost/ardour2/branches/3.0@4591 d708f5d6-7413-0410-9779-e7cbd77b26cf
Diffstat (limited to 'libs/evoral/evoral/midi_util.h')
-rw-r--r--libs/evoral/evoral/midi_util.h24
1 files changed, 16 insertions, 8 deletions
diff --git a/libs/evoral/evoral/midi_util.h b/libs/evoral/evoral/midi_util.h
index e290a4141d..8caf3fddc4 100644
--- a/libs/evoral/evoral/midi_util.h
+++ b/libs/evoral/evoral/midi_util.h
@@ -70,20 +70,22 @@ midi_event_size(uint8_t status)
return -1;
}
-/** Return the size of the given event including the status byte
- * (which must be the first byte in \a buffer),
- * or -1 if unknown (eg sysex)
+/** Return the size of the given event including the status byte,
+ * or -1 if event is illegal.
*/
static inline int
-midi_event_size(uint8_t* buffer)
+midi_event_size(const uint8_t* buffer)
{
uint8_t status = buffer[0];
- // if we have a channel event
+ // Mask off channel if applicable
if (status >= 0x80 && status < 0xF0) {
- status &= 0xF0; // mask off the channel
+ status &= 0xF0;
}
+ // FIXME: This is not correct, read the size and verify
+ // A sysex can contain the byte MIDI_CMD_COMMON_SYSEX_END, so this
+ // is likely to result in corrupt buffers and catastrophic failure
if (status == MIDI_CMD_COMMON_SYSEX) {
int end;
for (end = 1; buffer[end] != MIDI_CMD_COMMON_SYSEX_END; end++) {}
@@ -94,14 +96,20 @@ midi_event_size(uint8_t* buffer)
}
}
-/** Return true iff the given buffer is a valid MIDI event */
+/** Return true iff the given buffer is a valid MIDI event.
+ * \a len must be exactly correct for the contents of \a buffer
+ */
static inline bool
-midi_event_is_valid(uint8_t* buffer, size_t len)
+midi_event_is_valid(const uint8_t* buffer, size_t len)
{
uint8_t status = buffer[0];
if (status < 0x80) {
return false;
}
+ const int size = midi_event_size(buffer);
+ if (size < 0 || (size_t)size != len) {
+ return false;
+ }
return true;
}