Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(466)

Unified Diff: media/midi/midi_message_queue.cc

Issue 68353002: Use MIDIMessageQueue/IsValidWebMIDIData for MIDI byte stream validation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Use ScopedVector<T> instead to make legacy compilers happy. Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/midi/midi_message_queue.cc
diff --git a/media/midi/midi_message_queue.cc b/media/midi/midi_message_queue.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6bc9660c891183d534b62bca2e4c1f8fb754fcae
--- /dev/null
+++ b/media/midi/midi_message_queue.cc
@@ -0,0 +1,134 @@
+// Copyright (c) 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "media/midi/midi_message_queue.h"
+
+#include <algorithm>
+
+#include "base/logging.h"
+
+namespace media {
+namespace {
+
+const uint8 kSysEx = 0xf0;
+const uint8 kEndOfSysEx = 0xf7;
+
+bool IsDataByte(uint8 data) {
+ return data < 128;
+}
+
+bool IsFirstStatusByte(uint8 data) {
+ return !IsDataByte(data) && data != kEndOfSysEx;
+}
+
+bool IsSystemRealTimeMessage(uint8 data) {
+ return 0xf8 <= data && data <= 0xff;
+}
+
+size_t GetMessageLength(uint8 data) {
+ if (IsDataByte(data))
+ return 0;
+ else if (0x80 <= data && data <= 0xbf)
+ return 3;
+ else if (0xc0 <= data && data <= 0xdf)
+ return 2;
+ else if (0xe0 <= data && data <= 0xf0)
+ return 3;
+ else if (data == 0xf0)
+ return 0;
+ else if (data == 0xf1)
+ return 2;
+ else if (data == 0xf2)
+ return 3;
+ else if (data == 0xf3)
+ return 2;
+ else if (0xf4 <= data && data <= 0xf6)
+ return 1;
+ else if (data == 0xf7)
+ return 0;
+ else // 0xf8 <= data && data <= 0xff
+ return 1;
+}
+
+} // namespace
+
+MIDIMessageQueue::MIDIMessageQueue(bool allow_running_status)
+ : allow_running_status_(allow_running_status) {}
+
+MIDIMessageQueue::~MIDIMessageQueue() {}
+
+void MIDIMessageQueue::Add(const std::vector<uint8>& data) {
+ queue_.insert(queue_.end(), data.begin(), data.end());
+}
+
+void MIDIMessageQueue::Add(const uint8* data, size_t length) {
+ queue_.insert(queue_.end(), data, data + length);
+}
+
+void MIDIMessageQueue::Get(std::vector<uint8>* message) {
+ message->clear();
+
+ while (true) {
+ if (queue_.empty())
+ return;
+
+ const uint8 next = queue_.front();
+ queue_.pop_front();
+
+ // "System Real Time Messages" is a special kind of MIDI messages, which can
+ // appear at arbitrary byte position of MIDI stream. Here we reorder
+ // "System Real Time Messages" prior to |next_message_| so that each message
+ // can be clearly separated as a complete MIDI message.
+ if (IsSystemRealTimeMessage(next)) {
+ message->push_back(next);
+ return;
+ }
+
+ if (!next_message_.empty() &&
+ ((next_message_[0] == kSysEx && IsFirstStatusByte(next)) ||
+ (next_message_[0] != kSysEx && !IsDataByte(next)))) {
+ // Status byte appears. Clear the existing incomplete data.
+ next_message_.clear();
+ }
+
+ if (next_message_.empty()) {
+ if (IsFirstStatusByte(next)) {
+ next_message_.push_back(next);
+ } else {
+ // Invalid data. Ignore |next|.
+ }
+ continue;
+ }
+
+ next_message_.push_back(next);
+
+ DCHECK(!next_message_.empty());
+ const uint8 status_byte = next_message_[0];
+ if (status_byte == kSysEx) {
+ if (next == kEndOfSysEx) {
+ std::swap(*message, next_message_);
+ next_message_.clear();
+ return;
+ }
+ continue;
+ }
+
+ DCHECK(IsDataByte(next));
+ DCHECK_NE(kSysEx, status_byte);
+ const size_t target_len = GetMessageLength(status_byte);
+ if (next_message_.size() < target_len)
+ continue;
+ if (next_message_.size() == target_len) {
+ std::swap(*message, next_message_);
+ next_message_.clear();
+ if (allow_running_status_)
+ next_message_.push_back(status_byte);
+ return;
+ }
+
+ NOTREACHED();
+ }
+}
+
+} // namespace media

Powered by Google App Engine
This is Rietveld 408576698