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 /**
27  * fieldPrint is a helper method that logs to a stringstream a sequence of
28  * field names (in a fixed size array) together with a variable number of arg parameters.
29  *
30  * stringstream << field[0] << ":" << arg0 << " ";
31  * stringstream << field[1] << ":" << arg1 << " ";
32  * ...
33  * stringstream << field[N-1] << ":" << arg{N-1} << " ";
34  *
35  * The number of fields must exactly match the (variable) arguments.
36  *
37  * Example:
38  *
39  * const char * const fields[] = { "integer" };
40  * std::stringstream ss;
41  * fieldPrint(ss, fields, int(10));
42  */
43 template <size_t N, typename... Targs>
fieldPrint(std::stringstream & ss,const char * const (& fields)[N],Targs...args)44 void fieldPrint(std::stringstream& ss, const char * const (& fields)[N], Targs... args) {
45     static_assert(N == sizeof...(args));          // guarantee #fields == #args
46     auto fptr = fields;                           // get a pointer to the base of fields array
47     ((ss << *fptr++ << ":" << args << " "), ...); // (fold expression), send to stringstream.
48 }
49 
50 /**
51  * Return string tokens from iterator, separated by spaces and reserved chars.
52  */
53 std::string tokenizer(std::string::const_iterator& it,
54         const std::string::const_iterator& end, const char *reserved);
55 
56 /**
57  * Splits flags string based on delimeters (or, whitespace which is removed).
58  */
59 std::vector<std::string> split(const std::string& flags, const char *delim);
60 
61 /**
62  * Parse the devices string and return a vector of device address pairs.
63  *
64  * A failure to parse returns early with the contents that were able to be parsed.
65  */
66 std::vector<std::pair<std::string, std::string>> getDeviceAddressPairs(const std::string &devices);
67 
68 /**
69  * Replaces targetChars with replaceChar in string, returns number of chars replaced.
70  */
71 size_t replace(std::string &str, const char *targetChars, const char replaceChar);
72 
73 // RFC 1421, 2045, 2152, 4648(4), 4880
74 inline constexpr char Base64Table[] =
75     // 0000000000111111111122222222223333333333444444444455555555556666
76     // 0123456789012345678901234567890123456789012345678901234567890123
77     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
78 
79 // RFC 4648(5) URL-safe Base64 encoding
80 inline constexpr char Base64UrlTable[] =
81     // 0000000000111111111122222222223333333333444444444455555555556666
82     // 0123456789012345678901234567890123456789012345678901234567890123
83     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
84 
85 // An constexpr struct that transposes/inverts a string conversion table.
86 struct Transpose {
87     // constexpr bug, returning char still means -1 == 0xff, so we use unsigned char.
88     using base_char_t = unsigned char;
89     static inline constexpr base_char_t INVALID_CHAR = 0xff;
90 
91     template <size_t N>
TransposeTranspose92     explicit constexpr Transpose(const char(&string)[N]) {
93         for (auto& e : mMap) {
94             e = INVALID_CHAR;
95         }
96         for (size_t i = 0; string[i] != 0; ++i) {
97             mMap[static_cast<size_t>(string[i]) & 0xff] = i;
98         }
99     }
100 
101     constexpr base_char_t operator[] (size_t n) const {
102         return n < sizeof(mMap) ? mMap[n] : INVALID_CHAR;
103     }
104 
getTranspose105     constexpr const auto& get() const {
106         return mMap;
107     }
108 
109 private:
110     base_char_t mMap[256];  // construct an inverse character mapping.
111 };
112 
113 // This table is used to convert an input char to a 6 bit (0 - 63) value.
114 // If the input char is not in the Base64Url charset, Transpose::INVALID_CHAR is returned.
115 inline constexpr Transpose InverseBase64UrlTable(Base64UrlTable);
116 
117 // Returns true if s consists of only valid Base64Url characters (no padding chars allowed).
isBase64Url(const char * s)118 inline constexpr bool isBase64Url(const char *s) {
119     for (; *s != 0; ++s) {
120         if (InverseBase64UrlTable[(unsigned char)*s] == Transpose::INVALID_CHAR) return false;
121     }
122     return true;
123 }
124 
125 // Returns true if s is a valid log session id: exactly 16 Base64Url characters.
126 //
127 // logSessionIds are a web-safe Base64Url RFC 4648(5) encoded string of 16 characters
128 // (representing 96 unique bits 16 * 6).
129 //
130 // The string version is considered the reference representation.  However, for ease of
131 // manipulation and comparison, it may be converted to an int128.
132 //
133 // For int128 conversion, some common interpretations exist - for example
134 // (1) the 16 Base64 chars can be converted 6 bits per char to a 96 bit value
135 // (with the most significant 32 bits as zero) as there are only 12 unique bytes worth of data
136 // or (2) the 16 Base64 chars can be used to directly fill the 128 bits of int128 assuming
137 // the 16 chars are 16 bytes, filling the layout of the int128 variable.
138 // Endianness of the data may follow whatever is convenient in the interpretation as long
139 // as it is applied to each such conversion of string to int128 identically.
140 //
isLogSessionId(const char * s)141 inline constexpr bool isLogSessionId(const char *s) {
142     return std::char_traits<std::decay_t<decltype(*s)>>::length(s) == 16 && isBase64Url(s);
143 }
144 
145 // Returns either the original string or an empty string if isLogSessionId check fails.
sanitizeLogSessionId(const std::string & string)146 inline std::string sanitizeLogSessionId(const std::string& string) {
147     if (isLogSessionId(string.c_str())) return string;
148     return {}; // if not a logSessionId, return an empty string.
149 }
150 
151 inline std::string bytesToString(const std::vector<uint8_t>& bytes, size_t maxSize = SIZE_MAX) {
152     if (bytes.size() == 0) {
153         return "{}";
154     }
155     std::stringstream ss;
156     ss << "{";
157     ss << std::hex << std::setfill('0');
158     maxSize = std::min(maxSize, bytes.size());
159     for (size_t i = 0; i < maxSize; ++i) {
160         ss << " " << std::setw(2) << (int)bytes[i];
161     }
162     if (maxSize != bytes.size()) {
163         ss << " ... }";
164     } else {
165         ss << " }";
166     }
167     return ss.str();
168 }
169 
170 /**
171  * Returns true if the string is non-null, not empty, and contains only digits.
172  */
isNumeric(const char * s)173 inline constexpr bool isNumeric(const char *s)
174 {
175     if (s == nullptr || *s == 0) return false;
176     do {
177         if (!isdigit(*s)) return false;
178     } while (*++s != 0);
179     return true;  // all digits
180 }
181 
182 /**
183  * Extracts out the prefix from the key, returning a pair of prefix, suffix.
184  *
185  * Usually the key is something like:
186  * Prefix.(ID)
187  *   where ID is an integer,
188  *               or "error" if the id was not returned because of failure,
189  *               or "status" if general status.
190  *
191  * Example: audio.track.10     -> prefix = audio.track, suffix = 10
192  *          audio.track.error  -> prefix = audio.track, suffix = error
193  *          audio.track.status -> prefix = audio.track, suffix = status
194  *          audio.mute         -> prefix = audio.mute,  suffix = ""
195  */
196 inline std::pair<std::string /* prefix */,
splitPrefixKey(const std::string & key)197                  std::string /* suffix */> splitPrefixKey(const std::string &key)
198 {
199     const size_t split = key.rfind('.');
200     const char* suffix = key.c_str() + split + 1;
201     if (*suffix && (!strcmp(suffix, "error") || !strcmp(suffix, "status") || isNumeric(suffix))) {
202         return { key.substr(0, split), suffix };
203     }
204     return { key, "" };
205 }
206 
207 } // namespace android::mediametrics::stringutils
208