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

Side by Side Diff: media/midi/midi_message_util.cc

Issue 68353002: Use MIDIMessageQueue/IsValidWebMIDIData for MIDI byte stream validation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add IsValidMIDIMessagesForWebMIDI to avoid memory copy 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "media/midi/midi_message_util.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10
11 namespace media {
12 namespace {
13
14 const uint8 kSysEx = 0xf0;
15 const uint8 kEndOfSysEx = 0xf7;
16
17 bool IsDataByte(uint8 data) {
18 return (data & 0x80) == 0;
19 }
20
21 bool IsFirstStatusByte(uint8 data) {
22 return !IsDataByte(data) && data != kEndOfSysEx;
23 }
24
25 bool IsSystemRealTimeMessage(uint8 data) {
26 return 0xf8 <= data && data <= 0xff;
27 }
28
29 size_t GetMessageLength(uint8 data) {
30 if (IsDataByte(data))
31 return 0;
32 else if (0x80 <= data && data <= 0xbf)
33 return 3;
34 else if (0xc0 <= data && data <= 0xdf)
35 return 2;
36 else if (0xe0 <= data && data <= 0xef)
37 return 3;
38 else if (data == 0xf0)
39 return 0;
40 else if (data == 0xf1)
41 return 2;
42 else if (data == 0xf2)
43 return 3;
44 else if (data == 0xf3)
45 return 2;
46 else if (0xf4 <= data && data <= 0xf6)
47 return 1;
48 else if (data == 0xf7)
49 return 0;
50 else // 0xf8 <= data && data <= 0xff
51 return 1;
52 }
53
54 } // namespace
55
56 MIDIMessageQueue::MIDIMessageQueue(bool allow_running_status)
57 : allow_running_status_(allow_running_status) {}
58
59 MIDIMessageQueue::~MIDIMessageQueue() {}
60
61 void MIDIMessageQueue::Add(const std::vector<uint8>& data) {
62 queue_.insert(queue_.end(), data.begin(), data.end());
63 }
64
65 void MIDIMessageQueue::Add(const uint8* data, size_t length) {
66 queue_.insert(queue_.end(), data, data + length);
67 }
68
69 void MIDIMessageQueue::Get(std::vector<uint8>* message) {
70 message->clear();
71
72 while (true) {
73 if (queue_.empty())
74 return;
75
76 const uint8 next = queue_.front();
77 queue_.pop_front();
78
79 // "System Real Time Messages" is a special kind of MIDI messages, which can
80 // appear at arbitrary byte position of MIDI stream. Here we reorder
81 // "System Real Time Messages" prior to |next_message_| so that each message
82 // can be clearly separated as a complete MIDI message.
83 if (IsSystemRealTimeMessage(next)) {
84 message->push_back(next);
85 return;
86 }
87
88 // Here |next_message_[0]| may contain the previous status byte when
89 // |allow_running_status_| is true. Following condition fixes up
90 // |next_message_| if running status condition is not fulfilled.
91 if (!next_message_.empty() &&
92 ((next_message_[0] == kSysEx && IsFirstStatusByte(next)) ||
93 (next_message_[0] != kSysEx && !IsDataByte(next)))) {
94 // An invalid data sequence is found or running status condition is not
95 // fulfilled.
96 next_message_.clear();
97 }
98
99 if (next_message_.empty()) {
100 if (IsFirstStatusByte(next)) {
101 next_message_.push_back(next);
102 } else {
103 // MIDI protocol doesn't provide any error correction mechanism in
104 // physical layers, and incoming messages can be corrupted, and should
105 // be corrected here.
106 }
107 continue;
108 }
109
110 // Here we can assume |next_message_| starts with a valid status byte.
111 const uint8 status_byte = next_message_[0];
112 next_message_.push_back(next);
113
114 if (status_byte == kSysEx) {
115 if (next == kEndOfSysEx) {
116 std::swap(*message, next_message_);
117 next_message_.clear();
118 return;
119 }
120 continue;
121 }
122
123 DCHECK(IsDataByte(next));
124 DCHECK_NE(kSysEx, status_byte);
125 const size_t target_len = GetMessageLength(status_byte);
126 if (next_message_.size() < target_len)
127 continue;
128 if (next_message_.size() == target_len) {
129 std::swap(*message, next_message_);
130 next_message_.clear();
131 if (allow_running_status_) {
132 // Speculatively keep the status byte in case of running status. If this
133 // assumption is not true, |next_message_| will be cleared anyway.
134 next_message_.push_back(status_byte);
135 }
136 return;
137 }
138
139 NOTREACHED();
140 }
141 }
142
143 bool IsValidMIDIMessagesForWebMIDI(const std::vector<uint8>& data) {
144 bool in_sysex = false;
145 size_t waiting_data_length = 0;
146 for (size_t i = 0; i < data.size(); ++i) {
147 const uint8 current = data[i];
148 if (IsSystemRealTimeMessage(current))
149 continue; // Real time message can be placed at any point.
150 if (waiting_data_length > 0) {
151 if (!IsDataByte(current))
152 return false; // Error: |current| should have been data byte.
153 --waiting_data_length;
154 continue; // Found data byte as expected.
155 }
156 if (in_sysex) {
157 if (data[i] == kEndOfSysEx)
158 in_sysex = false;
159 else if (!IsDataByte(current))
160 return false; // Error: |current| should have been data byte.
161 continue; // Found data byte as expected.
162 }
163 if (current == kSysEx) {
164 in_sysex = true;
165 continue; // Found SysEX
166 }
167 waiting_data_length = GetMessageLength(current);
168 if (waiting_data_length == 0)
169 return false; // Error: |current| should have been a valid status byte.
170 --waiting_data_length; // Found status byte
171 }
172 return waiting_data_length == 0 && !in_sysex;
173 }
174
175 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698