1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #pragma once
18
19 #include <iomanip>
20 #include <sstream>
21 #include <string>
22 #include <vector>
23
24 namespace android::mediametrics::stringutils {
25
26 // Define a way of printing a vector - this
27 // is used for proto repeated arguments.
28 template <typename T>
29 inline std::ostream & operator<< (std::ostream& s,
30 std::vector<T> const& v) {
31 s << "{ ";
32 for (const auto& e : v) {
33 s << e << " ";
34 }
35 s << "}";
36 return s;
37 }
38
39 /**
40 * fieldPrint is a helper method that logs to a stringstream a sequence of
41 * field names (in a fixed size array) together with a variable number of arg parameters.
42 *
43 * stringstream << field[0] << ":" << arg0 << " ";
44 * stringstream << field[1] << ":" << arg1 << " ";
45 * ...
46 * stringstream << field[N-1] << ":" << arg{N-1} << " ";
47 *
48 * The number of fields must exactly match the (variable) arguments.
49 *
50 * Example:
51 *
52 * const char * const fields[] = { "integer" };
53 * std::stringstream ss;
54 * fieldPrint(ss, fields, int(10));
55 */
56 template <size_t N, typename... Targs>
fieldPrint(std::stringstream & ss,const char * const (& fields)[N],Targs...args)57 void fieldPrint(std::stringstream& ss, const char * const (& fields)[N], Targs... args) {
58 static_assert(N == sizeof...(args)); // guarantee #fields == #args
59 auto fptr = fields; // get a pointer to the base of fields array
60 ((ss << *fptr++ << ":" << args << " "), ...); // (fold expression), send to stringstream.
61 }
62
63 /**
64 * Return string tokens from iterator, separated by spaces and reserved chars.
65 */
66 std::string tokenizer(std::string::const_iterator& it,
67 const std::string::const_iterator& end, const char *reserved);
68
69 /**
70 * Splits flags string based on delimeters (or, whitespace which is removed).
71 */
72 std::vector<std::string> split(const std::string& flags, const char *delim);
73
74 /**
75 * Parse the devices string and return a vector of device address pairs.
76 *
77 * A failure to parse returns early with the contents that were able to be parsed.
78 */
79 std::vector<std::pair<std::string, std::string>> getDeviceAddressPairs(const std::string &devices);
80
81 /**
82 * Replaces targetChars with replaceChar in string, returns number of chars replaced.
83 */
84 size_t replace(std::string &str, const char *targetChars, const char replaceChar);
85
86 // RFC 1421, 2045, 2152, 4648(4), 4880
87 inline constexpr char Base64Table[] =
88 // 0000000000111111111122222222223333333333444444444455555555556666
89 // 0123456789012345678901234567890123456789012345678901234567890123
90 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
91
92 // RFC 4648(5) URL-safe Base64 encoding
93 inline constexpr char Base64UrlTable[] =
94 // 0000000000111111111122222222223333333333444444444455555555556666
95 // 0123456789012345678901234567890123456789012345678901234567890123
96 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
97
98 // An constexpr struct that transposes/inverts a string conversion table.
99 struct Transpose {
100 // constexpr bug, returning char still means -1 == 0xff, so we use unsigned char.
101 using base_char_t = unsigned char;
102 static inline constexpr base_char_t INVALID_CHAR = 0xff;
103
104 template <size_t N>
TransposeTranspose105 explicit constexpr Transpose(const char(&string)[N]) {
106 for (auto& e : mMap) {
107 e = INVALID_CHAR;
108 }
109 for (size_t i = 0; string[i] != 0; ++i) {
110 mMap[static_cast<size_t>(string[i]) & 0xff] = i;
111 }
112 }
113
114 constexpr base_char_t operator[] (size_t n) const {
115 return n < sizeof(mMap) ? mMap[n] : INVALID_CHAR;
116 }
117
getTranspose118 constexpr const auto& get() const {
119 return mMap;
120 }
121
122 private:
123 base_char_t mMap[256]; // construct an inverse character mapping.
124 };
125
126 // This table is used to convert an input char to a 6 bit (0 - 63) value.
127 // If the input char is not in the Base64Url charset, Transpose::INVALID_CHAR is returned.
128 inline constexpr Transpose InverseBase64UrlTable(Base64UrlTable);
129
130 // Returns true if s consists of only valid Base64Url characters (no padding chars allowed).
isBase64Url(const char * s)131 inline constexpr bool isBase64Url(const char *s) {
132 for (; *s != 0; ++s) {
133 if (InverseBase64UrlTable[(unsigned char)*s] == Transpose::INVALID_CHAR) return false;
134 }
135 return true;
136 }
137
138 // Returns true if s is a valid log session id: exactly 16 Base64Url characters.
139 //
140 // logSessionIds are a web-safe Base64Url RFC 4648(5) encoded string of 16 characters
141 // (representing 96 unique bits 16 * 6).
142 //
143 // The string version is considered the reference representation. However, for ease of
144 // manipulation and comparison, it may be converted to an int128.
145 //
146 // For int128 conversion, some common interpretations exist - for example
147 // (1) the 16 Base64 chars can be converted 6 bits per char to a 96 bit value
148 // (with the most significant 32 bits as zero) as there are only 12 unique bytes worth of data
149 // or (2) the 16 Base64 chars can be used to directly fill the 128 bits of int128 assuming
150 // the 16 chars are 16 bytes, filling the layout of the int128 variable.
151 // Endianness of the data may follow whatever is convenient in the interpretation as long
152 // as it is applied to each such conversion of string to int128 identically.
153 //
isLogSessionId(const char * s)154 inline constexpr bool isLogSessionId(const char *s) {
155 return std::char_traits<std::decay_t<decltype(*s)>>::length(s) == 16 && isBase64Url(s);
156 }
157
158 // Returns either the original string or an empty string if isLogSessionId check fails.
sanitizeLogSessionId(const std::string & string)159 inline std::string sanitizeLogSessionId(const std::string& string) {
160 if (isLogSessionId(string.c_str())) return string;
161 return {}; // if not a logSessionId, return an empty string.
162 }
163
164 inline std::string bytesToString(const std::vector<uint8_t>& bytes, size_t maxSize = SIZE_MAX) {
165 if (bytes.size() == 0) {
166 return "{}";
167 }
168 std::stringstream ss;
169 ss << "{";
170 ss << std::hex << std::setfill('0');
171 maxSize = std::min(maxSize, bytes.size());
172 for (size_t i = 0; i < maxSize; ++i) {
173 ss << " " << std::setw(2) << (int)bytes[i];
174 }
175 if (maxSize != bytes.size()) {
176 ss << " ... }";
177 } else {
178 ss << " }";
179 }
180 return ss.str();
181 }
182
183 /**
184 * Returns true if the string is non-null, not empty, and contains only digits.
185 */
isNumeric(const char * s)186 inline constexpr bool isNumeric(const char *s)
187 {
188 if (s == nullptr || *s == 0) return false;
189 do {
190 if (!isdigit(*s)) return false;
191 } while (*++s != 0);
192 return true; // all digits
193 }
194
195 /**
196 * Extracts out the prefix from the key, returning a pair of prefix, suffix.
197 *
198 * Usually the key is something like:
199 * Prefix.(ID)
200 * where ID is an integer,
201 * or "error" if the id was not returned because of failure,
202 * or "status" if general status.
203 *
204 * Example: audio.track.10 -> prefix = audio.track, suffix = 10
205 * audio.track.error -> prefix = audio.track, suffix = error
206 * audio.track.status -> prefix = audio.track, suffix = status
207 * audio.mute -> prefix = audio.mute, suffix = ""
208 */
209 inline std::pair<std::string /* prefix */,
splitPrefixKey(const std::string & key)210 std::string /* suffix */> splitPrefixKey(const std::string &key)
211 {
212 const size_t split = key.rfind('.');
213 const char* suffix = key.c_str() + split + 1;
214 if (*suffix && (!strcmp(suffix, "error") || !strcmp(suffix, "status") || isNumeric(suffix))) {
215 return { key.substr(0, split), suffix };
216 }
217 return { key, "" };
218 }
219
220 std::pair<std::string /* external statsd */, std::string /* internal */>
221 parseOutputDevicePairs(const std::string& outputDevicePairs);
222
223 std::pair<std::string /* external statsd */, std::string /* internal */>
224 parseInputDevicePairs(const std::string& inputDevicePairs);
225
hasBluetoothOutputDevice(std::string_view devices)226 inline bool hasBluetoothOutputDevice(std::string_view devices) {
227 return devices.find("AUDIO_DEVICE_OUT_BLUETOOTH") != std::string::npos;
228 }
229
230 } // namespace android::mediametrics::stringutils
231