Directory: | cvmfs/ |
---|---|
File: | cvmfs/util/string.cc |
Date: | 2024-04-28 02:33:07 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 282 | 295 | 95.6% |
Branches: | 266 | 385 | 69.1% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /** | ||
2 | * This file is part of the CernVM File System. | ||
3 | * | ||
4 | * Some common functions. | ||
5 | */ | ||
6 | |||
7 | #ifndef __STDC_FORMAT_MACROS | ||
8 | // NOLINTNEXTLINE | ||
9 | #define __STDC_FORMAT_MACROS | ||
10 | #endif | ||
11 | |||
12 | #include "string.h" | ||
13 | #include "cvmfs_config.h" | ||
14 | |||
15 | #include <errno.h> | ||
16 | #include <fcntl.h> | ||
17 | #include <inttypes.h> | ||
18 | #include <stdint.h> | ||
19 | #include <unistd.h> | ||
20 | |||
21 | #include <cstdio> | ||
22 | #include <cstdlib> | ||
23 | #include <cstring> | ||
24 | #include <ctime> | ||
25 | #include <string> | ||
26 | |||
27 | using namespace std; // NOLINT | ||
28 | |||
29 | #ifdef CVMFS_NAMESPACE_GUARD | ||
30 | namespace CVMFS_NAMESPACE_GUARD { | ||
31 | #endif | ||
32 | |||
33 | const char b64_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', | ||
34 | 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', | ||
35 | 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', | ||
36 | 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', | ||
37 | 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', | ||
38 | '3', '4', '5', '6', '7', '8', '9', '+', '/'}; | ||
39 | |||
40 | /** | ||
41 | * Decode Base64 and Base64Url | ||
42 | */ | ||
43 | const int8_t db64_table[] = { | ||
44 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
45 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
46 | -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, | ||
47 | 61, -1, -1, -1, 0, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, | ||
48 | 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, | ||
49 | 63, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, | ||
50 | 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, | ||
51 | |||
52 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
53 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
54 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
55 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
56 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
57 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
58 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
59 | }; | ||
60 | |||
61 | namespace { | ||
62 | |||
63 | /** | ||
64 | * Used for cas insensitive HasSuffix | ||
65 | */ | ||
66 | struct IgnoreCaseComperator { | ||
67 | 1009 | IgnoreCaseComperator() {} | |
68 | 21 | bool operator()(const std::string::value_type a, | |
69 | const std::string::value_type b) const { | ||
70 | 21 | return std::tolower(a) == std::tolower(b); | |
71 | } | ||
72 | }; | ||
73 | |||
74 | } // anonymous namespace | ||
75 | |||
76 |
3/4✓ Branch 1 taken 4 times.
✓ Branch 2 taken 98 times.
✓ Branch 4 taken 102 times.
✗ Branch 5 not taken.
|
102 | string StringifyBool(const bool value) { return value ? "yes" : "no"; } |
77 | |||
78 | 1033179 | string StringifyInt(const int64_t value) { | |
79 | char buffer[48]; | ||
80 | 1033179 | snprintf(buffer, sizeof(buffer), "%" PRId64, value); | |
81 |
1/2✓ Branch 2 taken 1036121 times.
✗ Branch 3 not taken.
|
1033179 | return string(buffer); |
82 | } | ||
83 | |||
84 | 366627 | std::string StringifyUint(const uint64_t value) { | |
85 | char buffer[48]; | ||
86 | 366627 | snprintf(buffer, sizeof(buffer), "%" PRIu64, value); | |
87 |
1/2✓ Branch 2 taken 366627 times.
✗ Branch 3 not taken.
|
366627 | return string(buffer); |
88 | } | ||
89 | |||
90 | 4 | string StringifyByteAsHex(const unsigned char value) { | |
91 | char buffer[3]; | ||
92 | 4 | snprintf(buffer, sizeof(buffer), "%02x", value); | |
93 |
1/2✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
|
4 | return string(buffer); |
94 | } | ||
95 | |||
96 | 8 | string StringifyDouble(const double value) { | |
97 | char buffer[64]; | ||
98 | 8 | snprintf(buffer, sizeof(buffer), "%.03f", value); | |
99 |
1/2✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
|
8 | return string(buffer); |
100 | } | ||
101 | |||
102 | /** | ||
103 | * Converts seconds since UTC 0 into something readable | ||
104 | */ | ||
105 | 95 | string StringifyTime(const time_t seconds, const bool utc) { | |
106 | struct tm timestamp; | ||
107 |
2/2✓ Branch 0 taken 62 times.
✓ Branch 1 taken 33 times.
|
95 | if (utc) { |
108 | 62 | localtime_r(&seconds, ×tamp); | |
109 | } else { | ||
110 | 33 | gmtime_r(&seconds, ×tamp); | |
111 | } | ||
112 | |||
113 | 95 | const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
114 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; | ||
115 | char buffer[21]; | ||
116 | 95 | snprintf(buffer, sizeof(buffer), "%d %s %d %02d:%02d:%02d", timestamp.tm_mday, | |
117 | 95 | months[timestamp.tm_mon], timestamp.tm_year + 1900, | |
118 | timestamp.tm_hour, timestamp.tm_min, timestamp.tm_sec); | ||
119 | |||
120 |
1/2✓ Branch 2 taken 95 times.
✗ Branch 3 not taken.
|
95 | return string(buffer); |
121 | } | ||
122 | |||
123 | |||
124 | /** | ||
125 | * Current time in format Wed, 01 Mar 2006 12:00:00 GMT | ||
126 | */ | ||
127 | 1399 | std::string RfcTimestamp() { | |
128 | 1399 | const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
129 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; | ||
130 | 1399 | const char *day_of_week[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; | |
131 | |||
132 | struct tm timestamp; | ||
133 | 1399 | time_t now = time(NULL); | |
134 | 1399 | gmtime_r(&now, ×tamp); | |
135 | |||
136 | char buffer[30]; | ||
137 | 1399 | snprintf(buffer, sizeof(buffer), "%s, %02d %s %d %02d:%02d:%02d %s", | |
138 | 1399 | day_of_week[timestamp.tm_wday], timestamp.tm_mday, | |
139 | 1399 | months[timestamp.tm_mon], timestamp.tm_year + 1900, | |
140 | timestamp.tm_hour, timestamp.tm_min, timestamp.tm_sec, | ||
141 | timestamp.tm_zone); | ||
142 |
1/2✓ Branch 2 taken 1399 times.
✗ Branch 3 not taken.
|
1399 | return string(buffer); |
143 | } | ||
144 | |||
145 | |||
146 | /** | ||
147 | * Current time in format YYYYMMDDTHHMMSSZ. Used in AWS4 requests. | ||
148 | */ | ||
149 | 1 | std::string IsoTimestamp() { | |
150 | struct tm timestamp; | ||
151 | 1 | time_t now = time(NULL); | |
152 | 1 | gmtime_r(&now, ×tamp); | |
153 | |||
154 | char buffer[17]; | ||
155 | 1 | snprintf(buffer, sizeof(buffer), "%04d%02d%02dT%02d%02d%02dZ", | |
156 | 1 | timestamp.tm_year + 1900, | |
157 | 1 | timestamp.tm_mon + 1, | |
158 | timestamp.tm_mday, | ||
159 | timestamp.tm_hour, | ||
160 | timestamp.tm_min, | ||
161 | timestamp.tm_sec); | ||
162 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | return string(buffer); |
163 | } | ||
164 | |||
165 | |||
166 | /** | ||
167 | * UTC time in format YYYYMMDDHHMMSS. Used in cvmfs whitelists. | ||
168 | */ | ||
169 | 7 | std::string WhitelistTimestamp(time_t when) { | |
170 | struct tm timestamp; | ||
171 | 7 | gmtime_r(&when, ×tamp); | |
172 | |||
173 | char buffer[15]; | ||
174 | 7 | snprintf(buffer, sizeof(buffer), "%04d%02d%02d%02d%02d%02d", | |
175 | 7 | timestamp.tm_year + 1900, | |
176 | 7 | timestamp.tm_mon + 1, | |
177 | timestamp.tm_mday, | ||
178 | timestamp.tm_hour, | ||
179 | timestamp.tm_min, | ||
180 | timestamp.tm_sec); | ||
181 |
1/2✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
|
7 | return string(buffer); |
182 | } | ||
183 | |||
184 | |||
185 | 372632 | string StringifyTimeval(const timeval value) { | |
186 | char buffer[64]; | ||
187 | 372632 | int64_t msec = value.tv_sec * 1000; | |
188 | 372632 | msec += value.tv_usec / 1000; | |
189 | 372632 | snprintf(buffer, sizeof(buffer), "%" PRId64 ".%03d", msec, | |
190 | 372632 | static_cast<int>(value.tv_usec % 1000)); | |
191 |
1/2✓ Branch 2 taken 372632 times.
✗ Branch 3 not taken.
|
372632 | return string(buffer); |
192 | } | ||
193 | |||
194 | /** | ||
195 | * Parses a timestamp of the form YYYY-MM-DDTHH:MM:SSZ | ||
196 | * Return 0 on error | ||
197 | */ | ||
198 | 108 | time_t IsoTimestamp2UtcTime(const std::string &iso8601) { | |
199 | 108 | time_t utc_time = 0; | |
200 | 108 | unsigned length = iso8601.length(); | |
201 | |||
202 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 106 times.
|
108 | if (length != 20) return utc_time; |
203 |
2/4✓ Branch 2 taken 106 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 106 times.
✗ Branch 6 not taken.
|
212 | if ((iso8601[4] != '-') || (iso8601[7] != '-') || (iso8601[10] != 'T') || |
204 |
5/10✓ Branch 0 taken 106 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 106 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 106 times.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 106 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 106 times.
|
212 | (iso8601[13] != ':') || (iso8601[16] != ':') || (iso8601[19] != 'Z')) { |
205 | ✗ | return utc_time; | |
206 | } | ||
207 | |||
208 | struct tm tm_wl; | ||
209 | 106 | memset(&tm_wl, 0, sizeof(struct tm)); | |
210 |
2/4✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
|
106 | tm_wl.tm_year = static_cast<int>(String2Int64(iso8601.substr(0, 4))) - 1900; |
211 |
2/4✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
|
106 | tm_wl.tm_mon = static_cast<int>(String2Int64(iso8601.substr(5, 2))) - 1; |
212 |
2/4✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
|
106 | tm_wl.tm_mday = static_cast<int>(String2Int64(iso8601.substr(8, 2))); |
213 |
2/4✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
|
106 | tm_wl.tm_hour = static_cast<int>(String2Int64(iso8601.substr(11, 2))); |
214 |
2/4✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
|
106 | tm_wl.tm_min = static_cast<int>(String2Int64(iso8601.substr(14, 2))); |
215 |
2/4✓ Branch 1 taken 106 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 106 times.
✗ Branch 5 not taken.
|
106 | tm_wl.tm_sec = static_cast<int>(String2Int64(iso8601.substr(17, 2))); |
216 | 106 | utc_time = timegm(&tm_wl); | |
217 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 105 times.
|
106 | if (utc_time < 0) return 0; |
218 | |||
219 | 105 | return utc_time; | |
220 | } | ||
221 | |||
222 | 1442 | int64_t String2Int64(const string &value) { | |
223 | int64_t result; | ||
224 | 1442 | sscanf(value.c_str(), "%" PRId64, &result); | |
225 | 1442 | return result; | |
226 | } | ||
227 | |||
228 | 7775 | uint64_t String2Uint64(const string &value) { | |
229 | uint64_t result; | ||
230 |
2/2✓ Branch 1 taken 7771 times.
✓ Branch 2 taken 4 times.
|
7775 | if (sscanf(value.c_str(), "%" PRIu64, &result) == 1) { |
231 | 7771 | return result; | |
232 | } | ||
233 | 4 | return 0; | |
234 | } | ||
235 | |||
236 | /** | ||
237 | * Parse a string into a a uint64_t. | ||
238 | * | ||
239 | * Unlike String2Uint64, this: | ||
240 | * - Checks to make sure the full string is parsed | ||
241 | * - Can indicate an error occurred. | ||
242 | * | ||
243 | * If an error occurs, this returns false and sets errno appropriately. | ||
244 | */ | ||
245 | 806 | bool String2Uint64Parse(const std::string &value, uint64_t *result) { | |
246 | 806 | char *endptr = NULL; | |
247 | 806 | errno = 0; | |
248 | 806 | long long myval = strtoll(value.c_str(), &endptr, 10); // NOLINT | |
249 |
8/8✓ Branch 1 taken 801 times.
✓ Branch 2 taken 5 times.
✓ Branch 5 taken 708 times.
✓ Branch 6 taken 93 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 707 times.
✓ Branch 9 taken 99 times.
✓ Branch 10 taken 707 times.
|
806 | if ((value.size() == 0) || (endptr != (value.c_str() + value.size())) || |
250 | (myval < 0)) { | ||
251 | 99 | errno = EINVAL; | |
252 | 99 | return false; | |
253 | } | ||
254 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 707 times.
|
707 | if (errno) { |
255 | ✗ | return false; | |
256 | } | ||
257 |
2/2✓ Branch 0 taken 706 times.
✓ Branch 1 taken 1 times.
|
707 | if (result) { |
258 | 706 | *result = myval; | |
259 | } | ||
260 | 707 | return true; | |
261 | } | ||
262 | |||
263 | 4 | void String2Uint64Pair(const string &value, uint64_t *a, uint64_t *b) { | |
264 | 4 | sscanf(value.c_str(), "%" PRIu64 " %" PRIu64, a, b); | |
265 | 4 | } | |
266 | |||
267 | 7844 | bool HasPrefix(const string &str, const string &prefix, | |
268 | const bool ignore_case) { | ||
269 |
2/2✓ Branch 2 taken 1672 times.
✓ Branch 3 taken 6172 times.
|
7844 | if (prefix.length() > str.length()) return false; |
270 | |||
271 |
2/2✓ Branch 1 taken 23454 times.
✓ Branch 2 taken 2144 times.
|
25598 | for (unsigned i = 0, l = prefix.length(); i < l; ++i) { |
272 |
2/2✓ Branch 0 taken 3466 times.
✓ Branch 1 taken 19988 times.
|
23454 | if (ignore_case) { |
273 |
2/2✓ Branch 2 taken 1671 times.
✓ Branch 3 taken 1795 times.
|
3466 | if (toupper(str[i]) != toupper(prefix[i])) return false; |
274 | } else { | ||
275 |
2/2✓ Branch 2 taken 2357 times.
✓ Branch 3 taken 17631 times.
|
19988 | if (str[i] != prefix[i]) return false; |
276 | } | ||
277 | } | ||
278 | 2144 | return true; | |
279 | } | ||
280 | |||
281 | 1019 | bool HasSuffix(const std::string &str, const std::string &suffix, | |
282 | const bool ignore_case) { | ||
283 |
2/2✓ Branch 2 taken 10 times.
✓ Branch 3 taken 1009 times.
|
1019 | if (suffix.size() > str.size()) return false; |
284 | 1009 | const IgnoreCaseComperator icmp; | |
285 | return (ignore_case) | ||
286 |
3/4✓ Branch 0 taken 13 times.
✓ Branch 1 taken 996 times.
✓ Branch 6 taken 13 times.
✗ Branch 7 not taken.
|
1009 | ? std::equal(suffix.rbegin(), suffix.rend(), str.rbegin(), icmp) |
287 |
1/2✓ Branch 4 taken 996 times.
✗ Branch 5 not taken.
|
1009 | : std::equal(suffix.rbegin(), suffix.rend(), str.rbegin()); |
288 | } | ||
289 | |||
290 | 1515 | vector<string> SplitString(const string &str, char delim) { | |
291 | 1515 | return SplitStringBounded(0, str, delim); | |
292 | } | ||
293 | |||
294 | 1521 | vector<string> SplitStringBounded( | |
295 | unsigned max_chunks, const string &str, char delim) | ||
296 | { | ||
297 | 1521 | vector<string> result; | |
298 | |||
299 | // edge case... one chunk is always the whole string | ||
300 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1520 times.
|
1521 | if (1 == max_chunks) { |
301 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | result.push_back(str); |
302 | 1 | return result; | |
303 | } | ||
304 | |||
305 | // split the string | ||
306 | 1520 | const unsigned size = str.size(); | |
307 | 1520 | unsigned marker = 0; | |
308 | 1520 | unsigned chunks = 1; | |
309 | unsigned i; | ||
310 |
2/2✓ Branch 0 taken 41007 times.
✓ Branch 1 taken 1518 times.
|
42525 | for (i = 0; i < size; ++i) { |
311 |
2/2✓ Branch 1 taken 1843 times.
✓ Branch 2 taken 39164 times.
|
41007 | if (str[i] == delim) { |
312 |
2/4✓ Branch 1 taken 1843 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1843 times.
✗ Branch 5 not taken.
|
1843 | result.push_back(str.substr(marker, i - marker)); |
313 | 1843 | marker = i + 1; | |
314 | |||
315 | // we got what we want... good bye | ||
316 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1841 times.
|
1843 | if (++chunks == max_chunks) break; |
317 | } | ||
318 | } | ||
319 | |||
320 | // push the remainings of the string and return | ||
321 |
2/4✓ Branch 1 taken 1520 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1520 times.
✗ Branch 5 not taken.
|
1520 | result.push_back(str.substr(marker)); |
322 | 1520 | return result; | |
323 | } | ||
324 | |||
325 | 320 | string JoinStrings(const vector<string> &strings, const string &joint) { | |
326 |
1/2✓ Branch 2 taken 320 times.
✗ Branch 3 not taken.
|
320 | string result = ""; |
327 | 320 | const unsigned size = strings.size(); | |
328 | |||
329 |
2/2✓ Branch 0 taken 239 times.
✓ Branch 1 taken 81 times.
|
320 | if (size > 0) { |
330 |
1/2✓ Branch 2 taken 239 times.
✗ Branch 3 not taken.
|
239 | result = strings[0]; |
331 |
4/7✓ Branch 2 taken 32 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 32 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 32 times.
✓ Branch 9 taken 239 times.
|
271 | for (unsigned i = 1; i < size; ++i) result += joint + strings[i]; |
332 | } | ||
333 | |||
334 | 320 | return result; | |
335 | } | ||
336 | |||
337 | 113 | void ParseKeyvalMem(const unsigned char *buffer, const unsigned buffer_size, | |
338 | map<char, string> *content) { | ||
339 | 113 | string line; | |
340 | 113 | unsigned pos = 0; | |
341 |
2/2✓ Branch 0 taken 9587 times.
✓ Branch 1 taken 2 times.
|
9589 | while (pos < buffer_size) { |
342 |
2/2✓ Branch 0 taken 750 times.
✓ Branch 1 taken 8837 times.
|
9587 | if (static_cast<char>(buffer[pos]) == '\n') { |
343 |
2/2✓ Branch 1 taken 111 times.
✓ Branch 2 taken 639 times.
|
750 | if (line == "--") return; |
344 | |||
345 |
1/2✓ Branch 1 taken 639 times.
✗ Branch 2 not taken.
|
639 | if (line != "") { |
346 |
3/11✗ Branch 1 not taken.
✓ Branch 2 taken 639 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 639 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 639 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
|
639 | const string tail = (line.length() == 1) ? "" : line.substr(1); |
347 | // Special handling of 'Z' key because it can exist multiple times | ||
348 |
3/4✓ Branch 1 taken 639 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 635 times.
✓ Branch 4 taken 4 times.
|
639 | if (line[0] != 'Z') { |
349 |
3/6✓ Branch 1 taken 635 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 635 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 635 times.
✗ Branch 8 not taken.
|
635 | (*content)[line[0]] = tail; |
350 | } else { | ||
351 |
4/7✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 2 times.
|
4 | if (content->find(line[0]) == content->end()) { |
352 |
3/6✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
|
2 | (*content)[line[0]] = tail; |
353 | } else { | ||
354 |
6/12✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 2 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 2 times.
✗ Branch 17 not taken.
|
2 | (*content)[line[0]] = (*content)[line[0]] + "|" + tail; |
355 | } | ||
356 | } | ||
357 | 639 | } | |
358 |
1/2✓ Branch 1 taken 639 times.
✗ Branch 2 not taken.
|
639 | line = ""; |
359 | } else { | ||
360 |
1/2✓ Branch 1 taken 8837 times.
✗ Branch 2 not taken.
|
8837 | line += static_cast<char>(buffer[pos]); |
361 | } | ||
362 | 9476 | pos++; | |
363 | } | ||
364 |
2/2✓ Branch 1 taken 2 times.
✓ Branch 2 taken 111 times.
|
113 | } |
365 | |||
366 | 12 | bool ParseKeyvalPath(const string &filename, map<char, string> *content) { | |
367 |
1/2✓ Branch 2 taken 12 times.
✗ Branch 3 not taken.
|
12 | int fd = open(filename.c_str(), O_RDONLY); |
368 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 11 times.
|
12 | if (fd < 0) return false; |
369 | |||
370 | unsigned char buffer[4096]; | ||
371 |
1/2✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
|
11 | ssize_t num_bytes = read(fd, buffer, sizeof(buffer)); |
372 |
1/2✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
|
11 | close(fd); |
373 | |||
374 |
3/4✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 10 times.
|
11 | if ((num_bytes <= 0) || (unsigned(num_bytes) >= sizeof(buffer))) return false; |
375 | |||
376 |
1/2✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
|
10 | ParseKeyvalMem(buffer, unsigned(num_bytes), content); |
377 | 10 | return true; | |
378 | } | ||
379 | |||
380 | 5805 | string GetLineMem(const char *text, const int text_size) { | |
381 | 5805 | int pos = 0; | |
382 |
4/4✓ Branch 0 taken 242861 times.
✓ Branch 1 taken 21 times.
✓ Branch 2 taken 237077 times.
✓ Branch 3 taken 5784 times.
|
242882 | while ((pos < text_size) && (text[pos] != '\n')) pos++; |
383 |
1/2✓ Branch 2 taken 5805 times.
✗ Branch 3 not taken.
|
5805 | return string(text, pos); |
384 | } | ||
385 | |||
386 | 374059 | bool GetLineFile(FILE *f, std::string *line) { | |
387 | int retval; | ||
388 | 374059 | line->clear(); | |
389 | while (true) { | ||
390 | 26640090 | retval = fgetc(f); | |
391 |
2/6✗ Branch 1 not taken.
✓ Branch 2 taken 26640090 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 26640090 times.
|
26640090 | if (ferror(f) && (errno == EINTR)) { |
392 | ✗ | clearerr(f); | |
393 | ✗ | continue; | |
394 |
2/2✓ Branch 0 taken 215 times.
✓ Branch 1 taken 26639875 times.
|
26640090 | } else if (retval == EOF) { |
395 | 215 | break; | |
396 | } | ||
397 | 26639875 | char c = static_cast<char>(retval); | |
398 |
2/2✓ Branch 0 taken 373844 times.
✓ Branch 1 taken 26266031 times.
|
26639875 | if (c == '\n') break; |
399 | 26266031 | line->push_back(c); | |
400 | 26266031 | } | |
401 |
4/4✓ Branch 0 taken 215 times.
✓ Branch 1 taken 373844 times.
✓ Branch 3 taken 37 times.
✓ Branch 4 taken 178 times.
|
374059 | return (retval != EOF) || !line->empty(); |
402 | } | ||
403 | |||
404 | 199 | bool GetLineFd(const int fd, std::string *line) { | |
405 | ssize_t retval; | ||
406 | char c; | ||
407 | 199 | line->clear(); | |
408 | while (true) { | ||
409 |
1/2✓ Branch 1 taken 9032 times.
✗ Branch 2 not taken.
|
9032 | retval = read(fd, &c, 1); |
410 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 9029 times.
|
9032 | if (retval == 0) { |
411 | 3 | break; | |
412 | } | ||
413 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 9029 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
9029 | if ((retval == -1) && (errno == EINTR)) { |
414 | ✗ | continue; | |
415 | } | ||
416 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9029 times.
|
9029 | if (retval == -1) { |
417 | ✗ | break; | |
418 | } | ||
419 |
2/2✓ Branch 0 taken 196 times.
✓ Branch 1 taken 8833 times.
|
9029 | if (c == '\n') break; |
420 |
1/2✓ Branch 1 taken 8833 times.
✗ Branch 2 not taken.
|
8833 | line->push_back(c); |
421 | } | ||
422 |
4/4✓ Branch 0 taken 3 times.
✓ Branch 1 taken 196 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1 times.
|
199 | return (retval == 1) || !line->empty(); |
423 | } | ||
424 | |||
425 | /** | ||
426 | * Removes leading and trailing whitespaces. | ||
427 | */ | ||
428 | 975 | string Trim(const string &raw, bool trim_newline) { | |
429 |
3/4✓ Branch 1 taken 83 times.
✓ Branch 2 taken 892 times.
✓ Branch 5 taken 83 times.
✗ Branch 6 not taken.
|
975 | if (raw.empty()) return ""; |
430 | |||
431 | 892 | unsigned start_pos = 0; | |
432 |
4/4✓ Branch 1 taken 961 times.
✓ Branch 2 taken 12 times.
✓ Branch 3 taken 81 times.
✓ Branch 4 taken 892 times.
|
1865 | for (; (start_pos < raw.length()) && |
433 |
5/6✓ Branch 1 taken 905 times.
✓ Branch 2 taken 56 times.
✓ Branch 4 taken 905 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 106 times.
✓ Branch 7 taken 799 times.
|
961 | (raw[start_pos] == ' ' || raw[start_pos] == '\t' || |
434 |
4/4✓ Branch 1 taken 94 times.
✓ Branch 2 taken 12 times.
✓ Branch 4 taken 13 times.
✓ Branch 5 taken 81 times.
|
106 | (trim_newline && (raw[start_pos] == '\n' || raw[start_pos] == '\r'))); |
435 | ++start_pos) | ||
436 | { | ||
437 | } | ||
438 | 892 | unsigned end_pos = raw.length() - 1; // at least one character in raw | |
439 | 1096 | for (; | |
440 |
4/4✓ Branch 0 taken 1084 times.
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 204 times.
✓ Branch 3 taken 892 times.
|
2180 | (end_pos >= start_pos) && |
441 |
5/6✓ Branch 1 taken 1029 times.
✓ Branch 2 taken 55 times.
✓ Branch 4 taken 1029 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 230 times.
✓ Branch 7 taken 799 times.
|
1084 | (raw[end_pos] == ' ' || raw[end_pos] == '\t' || |
442 |
4/4✓ Branch 1 taken 145 times.
✓ Branch 2 taken 85 times.
✓ Branch 4 taken 64 times.
✓ Branch 5 taken 81 times.
|
230 | (trim_newline && (raw[end_pos] == '\n' || raw[end_pos] == '\r'))); |
443 | --end_pos) | ||
444 | { | ||
445 | } | ||
446 | |||
447 | 892 | return raw.substr(start_pos, end_pos - start_pos + 1); | |
448 | } | ||
449 | |||
450 | 4 | std::string TrimString( | |
451 | const std::string& path, | ||
452 | const std::string& toTrim, | ||
453 | const int trimMode) | ||
454 | { | ||
455 | 4 | std::string trimmed = path; | |
456 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | if (trimmed != toTrim) { |
457 |
0/2✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
4 | while ((trimMode & kTrimLeading) && |
458 |
8/10✓ Branch 0 taken 8 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 8 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 6 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 6 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 6 times.
✓ Branch 10 taken 4 times.
|
16 | HasPrefix(trimmed, toTrim, true) && |
459 | 6 | (trimmed.size() > toTrim.size())) | |
460 | { | ||
461 |
1/2✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
|
6 | trimmed = trimmed.substr(toTrim.size()); |
462 | } | ||
463 |
0/2✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
4 | while ((trimMode & kTrimTrailing) && |
464 |
8/10✓ Branch 0 taken 6 times.
✓ Branch 1 taken 2 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
✓ Branch 6 taken 2 times.
✓ Branch 7 taken 4 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 4 times.
✓ Branch 10 taken 4 times.
|
12 | HasSuffix(trimmed, toTrim, true) && |
465 | 4 | (trimmed.size() > toTrim.size())) | |
466 | { | ||
467 |
1/2✓ Branch 3 taken 4 times.
✗ Branch 4 not taken.
|
4 | trimmed = trimmed.substr(0, trimmed.size() - toTrim.size()); |
468 | } | ||
469 | } | ||
470 | 4 | return trimmed; | |
471 | } | ||
472 | |||
473 | /** | ||
474 | * Converts all characters to upper case | ||
475 | */ | ||
476 | 97 | string ToUpper(const string &mixed_case) { | |
477 | 97 | string result(mixed_case); | |
478 |
2/2✓ Branch 1 taken 246 times.
✓ Branch 2 taken 97 times.
|
343 | for (unsigned i = 0, l = result.length(); i < l; ++i) { |
479 |
2/4✓ Branch 1 taken 246 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 246 times.
✗ Branch 5 not taken.
|
246 | result[i] = static_cast<char>(toupper(result[i])); |
480 | } | ||
481 | 97 | return result; | |
482 | } | ||
483 | |||
484 | 93 | string ReplaceAll(const string &haystack, const string &needle, | |
485 | const string &replace_by) { | ||
486 | 93 | string result(haystack); | |
487 | 93 | size_t pos = 0; | |
488 | 93 | const unsigned needle_size = needle.size(); | |
489 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 92 times.
|
93 | if (needle == "") return result; |
490 | |||
491 |
2/2✓ Branch 1 taken 62 times.
✓ Branch 2 taken 92 times.
|
154 | while ((pos = result.find(needle, pos)) != string::npos) |
492 |
1/2✓ Branch 1 taken 62 times.
✗ Branch 2 not taken.
|
62 | result.replace(pos, needle_size, replace_by); |
493 | 92 | return result; | |
494 | } | ||
495 | |||
496 | 16052 | static inline void Base64Block(const unsigned char input[3], const char *table, | |
497 | char output[4]) { | ||
498 | 16052 | output[0] = table[(input[0] & 0xFD) >> 2]; | |
499 | 16052 | output[1] = table[((input[0] & 0x03) << 4) | ((input[1] & 0xF0) >> 4)]; | |
500 | 16052 | output[2] = table[((input[1] & 0x0F) << 2) | ((input[2] & 0xD0) >> 6)]; | |
501 | 16052 | output[3] = table[input[2] & 0x3F]; | |
502 | 16052 | } | |
503 | |||
504 | 1878 | string Base64(const string &data) { | |
505 | 1878 | string result; | |
506 |
1/2✓ Branch 2 taken 1878 times.
✗ Branch 3 not taken.
|
1878 | result.reserve((data.length() + 3) * 4 / 3); |
507 | 1878 | unsigned pos = 0; | |
508 | const unsigned char *data_ptr = | ||
509 | 1878 | reinterpret_cast<const unsigned char *>(data.data()); | |
510 | 1878 | const unsigned length = data.length(); | |
511 |
2/2✓ Branch 0 taken 14191 times.
✓ Branch 1 taken 1878 times.
|
16069 | while (pos + 2 < length) { |
512 | char encoded_block[4]; | ||
513 | 14191 | Base64Block(data_ptr + pos, b64_table, encoded_block); | |
514 |
1/2✓ Branch 1 taken 14191 times.
✗ Branch 2 not taken.
|
14191 | result.append(encoded_block, 4); |
515 | 14191 | pos += 3; | |
516 | } | ||
517 |
2/2✓ Branch 0 taken 1861 times.
✓ Branch 1 taken 17 times.
|
1878 | if (length % 3 != 0) { |
518 | unsigned char input[3]; | ||
519 | 1861 | input[0] = data_ptr[pos]; | |
520 |
2/2✓ Branch 0 taken 1243 times.
✓ Branch 1 taken 618 times.
|
1861 | input[1] = ((length % 3) == 2) ? data_ptr[pos + 1] : 0; |
521 | 1861 | input[2] = 0; | |
522 | char encoded_block[4]; | ||
523 | 1861 | Base64Block(input, b64_table, encoded_block); | |
524 |
1/2✓ Branch 1 taken 1861 times.
✗ Branch 2 not taken.
|
1861 | result.append(encoded_block, 2); |
525 |
3/4✓ Branch 0 taken 1243 times.
✓ Branch 1 taken 618 times.
✓ Branch 3 taken 1861 times.
✗ Branch 4 not taken.
|
1861 | result.push_back(((length % 3) == 2) ? encoded_block[2] : '='); |
526 |
1/2✓ Branch 1 taken 1861 times.
✗ Branch 2 not taken.
|
1861 | result.push_back('='); |
527 | } | ||
528 | |||
529 | 1878 | return result; | |
530 | } | ||
531 | |||
532 | /** | ||
533 | * Safe encoding for URIs and path names: replace + by - and / by _ | ||
534 | */ | ||
535 | 5 | string Base64Url(const string &data) { | |
536 | 5 | string base64 = Base64(data); | |
537 |
2/2✓ Branch 1 taken 420 times.
✓ Branch 2 taken 5 times.
|
425 | for (unsigned i = 0, l = base64.length(); i < l; ++i) { |
538 |
3/4✓ Branch 1 taken 420 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 414 times.
|
420 | if (base64[i] == '+') { |
539 |
1/2✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
|
6 | base64[i] = '-'; |
540 |
3/4✓ Branch 1 taken 414 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 410 times.
|
414 | } else if (base64[i] == '/') { |
541 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
4 | base64[i] = '_'; |
542 | } | ||
543 | } | ||
544 | 5 | return base64; | |
545 | } | ||
546 | |||
547 | 29434 | static bool Debase64Block(const unsigned char input[4], unsigned char output[3]) | |
548 | { | ||
549 | int32_t dec[4]; | ||
550 |
2/2✓ Branch 0 taken 117733 times.
✓ Branch 1 taken 29433 times.
|
147166 | for (int i = 0; i < 4; ++i) { |
551 | 117733 | dec[i] = db64_table[input[i]]; | |
552 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 117732 times.
|
117733 | if (dec[i] < 0) return false; |
553 | } | ||
554 | |||
555 | 29433 | output[0] = (dec[0] << 2) | (dec[1] >> 4); | |
556 | 29433 | output[1] = ((dec[1] & 0x0F) << 4) | (dec[2] >> 2); | |
557 | 29433 | output[2] = ((dec[2] & 0x03) << 6) | dec[3]; | |
558 | 29433 | return true; | |
559 | } | ||
560 | |||
561 | /** | ||
562 | * Can decode both base64 and base64url | ||
563 | */ | ||
564 | 143 | bool Debase64(const string &data, string *decoded) { | |
565 | 143 | decoded->clear(); | |
566 | 143 | decoded->reserve((data.length() + 4) * 3 / 4); | |
567 | 143 | unsigned pos = 0; | |
568 | const unsigned char *data_ptr = | ||
569 | 143 | reinterpret_cast<const unsigned char *>(data.data()); | |
570 | 143 | const unsigned length = data.length(); | |
571 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 142 times.
|
143 | if (length == 0) return true; |
572 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 140 times.
|
142 | if ((length % 4) != 0) return false; |
573 | |||
574 |
2/2✓ Branch 0 taken 29434 times.
✓ Branch 1 taken 139 times.
|
29573 | while (pos < length) { |
575 | unsigned char decoded_block[3]; | ||
576 | 29434 | bool retval = Debase64Block(data_ptr + pos, decoded_block); | |
577 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 29433 times.
|
29434 | if (!retval) return false; |
578 |
1/2✓ Branch 1 taken 29433 times.
✗ Branch 2 not taken.
|
29433 | decoded->append(reinterpret_cast<char *>(decoded_block), 3); |
579 | 29433 | pos += 4; | |
580 | } | ||
581 | |||
582 |
2/2✓ Branch 0 taken 278 times.
✓ Branch 1 taken 139 times.
|
417 | for (int i = 0; i < 2; ++i) { |
583 | 278 | pos--; | |
584 |
2/2✓ Branch 1 taken 50 times.
✓ Branch 2 taken 228 times.
|
278 | if (data[pos] == '=') decoded->erase(decoded->length() - 1); |
585 | } | ||
586 | 139 | return true; | |
587 | } | ||
588 | |||
589 | /** | ||
590 | * Assumes that source is terminated by a newline | ||
591 | */ | ||
592 | 7 | string Tail(const string &source, unsigned num_lines) { | |
593 |
7/8✓ Branch 1 taken 5 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 4 times.
✓ Branch 5 taken 3 times.
✓ Branch 6 taken 4 times.
✓ Branch 9 taken 3 times.
✗ Branch 10 not taken.
|
7 | if (source.empty() || (num_lines == 0)) return ""; |
594 | |||
595 | 4 | int l = static_cast<int>(source.length()); | |
596 | 4 | int i = l - 1; | |
597 |
2/2✓ Branch 0 taken 16 times.
✓ Branch 1 taken 3 times.
|
19 | for (; i >= 0; --i) { |
598 | 16 | char c = source.data()[i]; | |
599 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 11 times.
|
16 | if (c == '\n') { |
600 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 4 times.
|
5 | if (num_lines == 0) { |
601 | 1 | return source.substr(i + 1); | |
602 | } | ||
603 | 4 | num_lines--; | |
604 | } | ||
605 | } | ||
606 | 3 | return source; | |
607 | } | ||
608 | |||
609 | /** | ||
610 | * Get UTC Time. | ||
611 | * | ||
612 | * @param format format if timestamp (YYYY-MM-DD HH:MM:SS by default) | ||
613 | * @return a timestamp string on success, empty string on failure | ||
614 | */ | ||
615 | ✗ | std::string GetGMTimestamp(const std::string &format) { | |
616 | struct tm time_ptr; | ||
617 | char date_and_time[100]; | ||
618 | ✗ | time_t t = time(NULL); | |
619 | ✗ | gmtime_r(&t, &time_ptr); // take UTC | |
620 | // return empty string if formatting fails | ||
621 | ✗ | if (!strftime(date_and_time, 100, format.c_str(), &time_ptr)) { | |
622 | ✗ | return ""; | |
623 | } | ||
624 | ✗ | std::string timestamp(date_and_time); | |
625 | ✗ | return timestamp; | |
626 | } | ||
627 | |||
628 | #ifdef CVMFS_NAMESPACE_GUARD | ||
629 | } // namespace CVMFS_NAMESPACE_GUARD | ||
630 | #endif | ||
631 |