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

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

Issue 2431393002: Web MIDI: move MIDI message validating logic into message_util.{cc|h} (Closed)
Patch Set: build fix Created 4 years, 2 months 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
« no previous file with comments | « media/midi/message_util.h ('k') | media/midi/message_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 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/message_util.h"
6
7 #include "base/logging.h"
8 #include "base/metrics/histogram_macros.h"
9
10 namespace midi {
11
12 size_t GetMessageLength(uint8_t status_byte) {
13 if (status_byte < 0x80)
14 return 0;
15 if (0x80 <= status_byte && status_byte <= 0xbf)
16 return 3;
17 if (0xc0 <= status_byte && status_byte <= 0xdf)
18 return 2;
19 if (0xe0 <= status_byte && status_byte <= 0xef)
20 return 3;
21
22 switch (status_byte) {
23 case 0xf0:
24 return 0;
25 case 0xf1:
26 return 2;
27 case 0xf2:
28 return 3;
29 case 0xf3:
30 return 2;
31 case 0xf4: // Reserved
32 case 0xf5: // Reserved
33 return 0;
34 case 0xf6:
35 return 1;
36 case 0xf7:
37 return 0;
38 case 0xf8:
39 case 0xf9:
40 case 0xfa:
41 case 0xfb:
42 case 0xfc:
43 case 0xfd:
44 case 0xfe:
45 case 0xff:
46 return 1;
47 default:
48 NOTREACHED();
49 return 0;
50 }
51 }
52
53 bool IsDataByte(uint8_t data) {
54 return (data & 0x80) == 0;
55 }
56
57 bool IsSystemRealTimeMessage(uint8_t data) {
58 return 0xf8 <= data;
59 }
60
61 bool IsSystemMessage(uint8_t data) {
62 return 0xf0 <= data;
63 }
64
65 bool IsValidWebMIDIData(const std::vector<uint8_t>& data) {
66 bool in_sysex = false;
67 size_t sysex_start_offset = 0;
68 size_t waiting_data_length = 0;
69 for (size_t i = 0; i < data.size(); ++i) {
70 const uint8_t current = data[i];
71 if (IsSystemRealTimeMessage(current))
72 continue; // Real time message can be placed at any point.
73 if (waiting_data_length > 0) {
74 if (!IsDataByte(current))
75 return false; // Error: |current| should have been data byte.
76 --waiting_data_length;
77 continue; // Found data byte as expected.
78 }
79 if (in_sysex) {
80 if (data[i] == kEndOfSysExByte) {
81 in_sysex = false;
82 UMA_HISTOGRAM_COUNTS("Media.Midi.SysExMessageSizeUpTo1MB",
83 static_cast<base::HistogramBase::Sample>(
84 i - sysex_start_offset + 1));
85 } else if (!IsDataByte(current)) {
86 return false; // Error: |current| should have been data byte.
87 }
88 continue; // Found data byte as expected.
89 }
90 if (current == kSysExByte) {
91 in_sysex = true;
92 sysex_start_offset = i;
93 continue; // Found SysEX
94 }
95 waiting_data_length = GetMessageLength(current);
96 if (waiting_data_length == 0)
97 return false; // Error: |current| should have been a valid status byte.
98 --waiting_data_length; // Found status byte
99 }
100 return waiting_data_length == 0 && !in_sysex;
101 }
102
103 } // namespace midi
OLDNEW
« no previous file with comments | « media/midi/message_util.h ('k') | media/midi/message_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698