blob: 01034d929859f979c17468cde4d69d50a2f44edc [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
Andy Hung1f560f82021-03-31 19:08:52 -070019#include <iomanip>
20#include <sstream>
Andy Hung1ea842e2020-05-18 10:47:31 -070021#include <string>
22#include <vector>
23
24namespace android::mediametrics::stringutils {
25
26/**
Andy Hunga629bd12020-06-05 16:03:53 -070027 * 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 */
43template <size_t N, typename... Targs>
44void 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/**
Andy Hung1ea842e2020-05-18 10:47:31 -070051 * Return string tokens from iterator, separated by spaces and reserved chars.
52 */
53std::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 */
59std::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 */
66std::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 */
71size_t replace(std::string &str, const char *targetChars, const char replaceChar);
72
Andy Hungcbcfaa22021-02-23 13:54:49 -080073// RFC 1421, 2045, 2152, 4648(4), 4880
74inline constexpr char Base64Table[] =
75 // 0000000000111111111122222222223333333333444444444455555555556666
76 // 0123456789012345678901234567890123456789012345678901234567890123
77 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
78
79// RFC 4648(5) URL-safe Base64 encoding
80inline constexpr char Base64UrlTable[] =
81 // 0000000000111111111122222222223333333333444444444455555555556666
82 // 0123456789012345678901234567890123456789012345678901234567890123
83 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
84
85// An constexpr struct that transposes/inverts a string conversion table.
86struct 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>
92 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
105 constexpr const auto& get() const {
106 return mMap;
107 }
108
109private:
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.
115inline constexpr Transpose InverseBase64UrlTable(Base64UrlTable);
116
117// Returns true if s consists of only valid Base64Url characters (no padding chars allowed).
118inline 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//
141inline 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.
146inline 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
Andy Hung1f560f82021-03-31 19:08:52 -0700151inline 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
Andy Hung1ea842e2020-05-18 10:47:31 -0700170} // namespace android::mediametrics::stringutils