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 * Replaces targetChars with replaceChar in string, returns number of chars replaced.
65 */
66 size_t replace(std::string &str, const char *targetChars, const char replaceChar);
67
68 // RFC 1421, 2045, 2152, 4648(4), 4880
69 inline constexpr char Base64Table[] =
70 // 0000000000111111111122222222223333333333444444444455555555556666
71 // 0123456789012345678901234567890123456789012345678901234567890123
72 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
73
74 // RFC 4648(5) URL-safe Base64 encoding
75 inline constexpr char Base64UrlTable[] =
76 // 0000000000111111111122222222223333333333444444444455555555556666
77 // 0123456789012345678901234567890123456789012345678901234567890123
78 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
79
80 // An constexpr struct that transposes/inverts a string conversion table.
81 struct Transpose {
82 // constexpr bug, returning char still means -1 == 0xff, so we use unsigned char.
83 using base_char_t = unsigned char;
84 static inline constexpr base_char_t INVALID_CHAR = 0xff;
85
86 template <size_t N>
TransposeTranspose87 explicit constexpr Transpose(const char(&string)[N]) {
88 for (auto& e : mMap) {
89 e = INVALID_CHAR;
90 }
91 for (size_t i = 0; string[i] != 0; ++i) {
92 mMap[static_cast<size_t>(string[i]) & 0xff] = i;
93 }
94 }
95
96 constexpr base_char_t operator[] (size_t n) const {
97 return n < sizeof(mMap) ? mMap[n] : INVALID_CHAR;
98 }
99
getTranspose100 constexpr const auto& get() const {
101 return mMap;
102 }
103
104 private:
105 base_char_t mMap[256]; // construct an inverse character mapping.
106 };
107
108 // This table is used to convert an input char to a 6 bit (0 - 63) value.
109 // If the input char is not in the Base64Url charset, Transpose::INVALID_CHAR is returned.
110 inline constexpr Transpose InverseBase64UrlTable(Base64UrlTable);
111
112 // Returns true if s consists of only valid Base64Url characters (no padding chars allowed).
isBase64Url(const char * s)113 inline constexpr bool isBase64Url(const char *s) {
114 for (; *s != 0; ++s) {
115 if (InverseBase64UrlTable[(unsigned char)*s] == Transpose::INVALID_CHAR) return false;
116 }
117 return true;
118 }
119
120 // Returns true if s is a valid log session id: exactly 16 Base64Url characters.
121 //
122 // logSessionIds are a web-safe Base64Url RFC 4648(5) encoded string of 16 characters
123 // (representing 96 unique bits 16 * 6).
124 //
125 // The string version is considered the reference representation. However, for ease of
126 // manipulation and comparison, it may be converted to an int128.
127 //
128 // For int128 conversion, some common interpretations exist - for example
129 // (1) the 16 Base64 chars can be converted 6 bits per char to a 96 bit value
130 // (with the most significant 32 bits as zero) as there are only 12 unique bytes worth of data
131 // or (2) the 16 Base64 chars can be used to directly fill the 128 bits of int128 assuming
132 // the 16 chars are 16 bytes, filling the layout of the int128 variable.
133 // Endianness of the data may follow whatever is convenient in the interpretation as long
134 // as it is applied to each such conversion of string to int128 identically.
135 //
isLogSessionId(const char * s)136 inline constexpr bool isLogSessionId(const char *s) {
137 return std::char_traits<std::decay_t<decltype(*s)>>::length(s) == 16 && isBase64Url(s);
138 }
139
140 // Returns either the original string or an empty string if isLogSessionId check fails.
sanitizeLogSessionId(const std::string & string)141 inline std::string sanitizeLogSessionId(const std::string& string) {
142 if (isLogSessionId(string.c_str())) return string;
143 return {}; // if not a logSessionId, return an empty string.
144 }
145
146 inline std::string bytesToString(const std::vector<uint8_t>& bytes, size_t maxSize = SIZE_MAX) {
147 if (bytes.size() == 0) {
148 return "{}";
149 }
150 std::stringstream ss;
151 ss << "{";
152 ss << std::hex << std::setfill('0');
153 maxSize = std::min(maxSize, bytes.size());
154 for (size_t i = 0; i < maxSize; ++i) {
155 ss << " " << std::setw(2) << (int)bytes[i];
156 }
157 if (maxSize != bytes.size()) {
158 ss << " ... }";
159 } else {
160 ss << " }";
161 }
162 return ss.str();
163 }
164
165 /**
166 * Returns true if the string is non-null, not empty, and contains only digits.
167 */
isNumeric(const char * s)168 inline constexpr bool isNumeric(const char *s)
169 {
170 if (s == nullptr || *s == 0) return false;
171 do {
172 if (!isdigit(*s)) return false;
173 } while (*++s != 0);
174 return true; // all digits
175 }
176
177 /**
178 * Extracts out the prefix from the key, returning a pair of prefix, suffix.
179 *
180 * Usually the key is something like:
181 * Prefix.(ID)
182 * where ID is an integer,
183 * or "error" if the id was not returned because of failure,
184 * or "status" if general status.
185 *
186 * Example: audio.track.10 -> prefix = audio.track, suffix = 10
187 * audio.track.error -> prefix = audio.track, suffix = error
188 * audio.track.status -> prefix = audio.track, suffix = status
189 * audio.mute -> prefix = audio.mute, suffix = ""
190 */
191 inline std::pair<std::string /* prefix */,
splitPrefixKey(const std::string & key)192 std::string /* suffix */> splitPrefixKey(const std::string &key)
193 {
194 const size_t split = key.rfind('.');
195 const char* suffix = key.c_str() + split + 1;
196 if (*suffix && (!strcmp(suffix, "error") || !strcmp(suffix, "status") || isNumeric(suffix))) {
197 return { key.substr(0, split), suffix };
198 }
199 return { key, "" };
200 }
201
202 std::pair<std::string /* external statsd */, std::string /* internal */>
203 parseOutputDevicePairs(const std::string& outputDevicePairs);
204
205 std::pair<std::string /* external statsd */, std::string /* internal */>
206 parseInputDevicePairs(const std::string& inputDevicePairs);
207
hasBluetoothOutputDevice(std::string_view devices)208 inline bool hasBluetoothOutputDevice(std::string_view devices) {
209 return devices.find("AUDIO_DEVICE_OUT_BLUETOOTH") != std::string::npos;
210 }
211
212 } // namespace android::mediametrics::stringutils
213