blob: 37ed173cdea9985017dcffc75f8953e680167645 [file] [log] [blame]
Andy Hung1ea842e2020-05-18 10:47:31 -07001/*
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 <string>
20#include <vector>
21
22namespace android::mediametrics::stringutils {
23
24/**
Andy Hunga629bd12020-06-05 16:03:53 -070025 * fieldPrint is a helper method that logs to a stringstream a sequence of
26 * field names (in a fixed size array) together with a variable number of arg parameters.
27 *
28 * stringstream << field[0] << ":" << arg0 << " ";
29 * stringstream << field[1] << ":" << arg1 << " ";
30 * ...
31 * stringstream << field[N-1] << ":" << arg{N-1} << " ";
32 *
33 * The number of fields must exactly match the (variable) arguments.
34 *
35 * Example:
36 *
37 * const char * const fields[] = { "integer" };
38 * std::stringstream ss;
39 * fieldPrint(ss, fields, int(10));
40 */
41template <size_t N, typename... Targs>
42void fieldPrint(std::stringstream& ss, const char * const (& fields)[N], Targs... args) {
43 static_assert(N == sizeof...(args)); // guarantee #fields == #args
44 auto fptr = fields; // get a pointer to the base of fields array
45 ((ss << *fptr++ << ":" << args << " "), ...); // (fold expression), send to stringstream.
46}
47
48/**
Andy Hung1ea842e2020-05-18 10:47:31 -070049 * Return string tokens from iterator, separated by spaces and reserved chars.
50 */
51std::string tokenizer(std::string::const_iterator& it,
52 const std::string::const_iterator& end, const char *reserved);
53
54/**
55 * Splits flags string based on delimeters (or, whitespace which is removed).
56 */
57std::vector<std::string> split(const std::string& flags, const char *delim);
58
59/**
60 * Parse the devices string and return a vector of device address pairs.
61 *
62 * A failure to parse returns early with the contents that were able to be parsed.
63 */
64std::vector<std::pair<std::string, std::string>> getDeviceAddressPairs(const std::string &devices);
65
66/**
67 * Replaces targetChars with replaceChar in string, returns number of chars replaced.
68 */
69size_t replace(std::string &str, const char *targetChars, const char replaceChar);
70
Andy Hungcbcfaa22021-02-23 13:54:49 -080071// RFC 1421, 2045, 2152, 4648(4), 4880
72inline constexpr char Base64Table[] =
73 // 0000000000111111111122222222223333333333444444444455555555556666
74 // 0123456789012345678901234567890123456789012345678901234567890123
75 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
76
77// RFC 4648(5) URL-safe Base64 encoding
78inline constexpr char Base64UrlTable[] =
79 // 0000000000111111111122222222223333333333444444444455555555556666
80 // 0123456789012345678901234567890123456789012345678901234567890123
81 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
82
83// An constexpr struct that transposes/inverts a string conversion table.
84struct Transpose {
85 // constexpr bug, returning char still means -1 == 0xff, so we use unsigned char.
86 using base_char_t = unsigned char;
87 static inline constexpr base_char_t INVALID_CHAR = 0xff;
88
89 template <size_t N>
90 explicit constexpr Transpose(const char(&string)[N]) {
91 for (auto& e : mMap) {
92 e = INVALID_CHAR;
93 }
94 for (size_t i = 0; string[i] != 0; ++i) {
95 mMap[static_cast<size_t>(string[i]) & 0xff] = i;
96 }
97 }
98
99 constexpr base_char_t operator[] (size_t n) const {
100 return n < sizeof(mMap) ? mMap[n] : INVALID_CHAR;
101 }
102
103 constexpr const auto& get() const {
104 return mMap;
105 }
106
107private:
108 base_char_t mMap[256]; // construct an inverse character mapping.
109};
110
111// This table is used to convert an input char to a 6 bit (0 - 63) value.
112// If the input char is not in the Base64Url charset, Transpose::INVALID_CHAR is returned.
113inline constexpr Transpose InverseBase64UrlTable(Base64UrlTable);
114
115// Returns true if s consists of only valid Base64Url characters (no padding chars allowed).
116inline constexpr bool isBase64Url(const char *s) {
117 for (; *s != 0; ++s) {
118 if (InverseBase64UrlTable[(unsigned char)*s] == Transpose::INVALID_CHAR) return false;
119 }
120 return true;
121}
122
123// Returns true if s is a valid log session id: exactly 16 Base64Url characters.
124//
125// logSessionIds are a web-safe Base64Url RFC 4648(5) encoded string of 16 characters
126// (representing 96 unique bits 16 * 6).
127//
128// The string version is considered the reference representation. However, for ease of
129// manipulation and comparison, it may be converted to an int128.
130//
131// For int128 conversion, some common interpretations exist - for example
132// (1) the 16 Base64 chars can be converted 6 bits per char to a 96 bit value
133// (with the most significant 32 bits as zero) as there are only 12 unique bytes worth of data
134// or (2) the 16 Base64 chars can be used to directly fill the 128 bits of int128 assuming
135// the 16 chars are 16 bytes, filling the layout of the int128 variable.
136// Endianness of the data may follow whatever is convenient in the interpretation as long
137// as it is applied to each such conversion of string to int128 identically.
138//
139inline constexpr bool isLogSessionId(const char *s) {
140 return std::char_traits<std::decay_t<decltype(*s)>>::length(s) == 16 && isBase64Url(s);
141}
142
143// Returns either the original string or an empty string if isLogSessionId check fails.
144inline std::string sanitizeLogSessionId(const std::string& string) {
145 if (isLogSessionId(string.c_str())) return string;
146 return {}; // if not a logSessionId, return an empty string.
147}
148
Andy Hung1ea842e2020-05-18 10:47:31 -0700149} // namespace android::mediametrics::stringutils