| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/util/string.cc |
| Date: | 2026-05-24 02:35:55 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 319 | 332 | 96.1% |
| Branches: | 273 | 392 | 69.6% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | * | ||
| 4 | * Some common functions. | ||
| 5 | */ | ||
| 6 | |||
| 7 | #include "string.h" | ||
| 8 | |||
| 9 | #include <cctype> | ||
| 10 | #include <cstdio> | ||
| 11 | #include <cstdlib> | ||
| 12 | #include <cstring> | ||
| 13 | #include <ctime> | ||
| 14 | #include <algorithm> | ||
| 15 | #include <vector> | ||
| 16 | #include <map> | ||
| 17 | #include <stdio.h> | ||
| 18 | #include <errno.h> | ||
| 19 | #include <fcntl.h> | ||
| 20 | #include <inttypes.h> | ||
| 21 | #include <stdint.h> | ||
| 22 | #include <time.h> | ||
| 23 | #include <string> | ||
| 24 | #include <unistd.h> | ||
| 25 | |||
| 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 | 12945 | IgnoreCaseComperator() { } | |
| 68 | 505 | bool operator()(const std::string::value_type a, | |
| 69 | const std::string::value_type b) const { | ||
| 70 | 505 | return std::tolower(a) == std::tolower(b); | |
| 71 | } | ||
| 72 | }; | ||
| 73 | |||
| 74 | } // anonymous namespace | ||
| 75 | |||
| 76 |
3/4✓ Branch 1 taken 124 times.
✓ Branch 2 taken 2310 times.
✓ Branch 4 taken 2434 times.
✗ Branch 5 not taken.
|
2434 | string StringifyBool(const bool value) { return value ? "yes" : "no"; } |
| 77 | |||
| 78 | 37622080 | string StringifyInt(const int64_t value) { | |
| 79 | char buffer[48]; | ||
| 80 | 37622080 | snprintf(buffer, sizeof(buffer), "%" PRId64, value); | |
| 81 |
1/2✓ Branch 2 taken 37304992 times.
✗ Branch 3 not taken.
|
37622080 | return string(buffer); |
| 82 | } | ||
| 83 | |||
| 84 | 3666854 | std::string StringifyUint(const uint64_t value) { | |
| 85 | char buffer[48]; | ||
| 86 | 3666854 | snprintf(buffer, sizeof(buffer), "%" PRIu64, value); | |
| 87 |
1/2✓ Branch 2 taken 3666854 times.
✗ Branch 3 not taken.
|
3666854 | return string(buffer); |
| 88 | } | ||
| 89 | |||
| 90 | 156 | string StringifyByteAsHex(const unsigned char value) { | |
| 91 | char buffer[3]; | ||
| 92 | 156 | snprintf(buffer, sizeof(buffer), "%02x", value); | |
| 93 |
1/2✓ Branch 2 taken 156 times.
✗ Branch 3 not taken.
|
156 | return string(buffer); |
| 94 | } | ||
| 95 | |||
| 96 | 150 | string StringifyDouble(const double value) { | |
| 97 | char buffer[64]; | ||
| 98 | 150 | snprintf(buffer, sizeof(buffer), "%.03f", value); | |
| 99 |
1/2✓ Branch 2 taken 150 times.
✗ Branch 3 not taken.
|
150 | return string(buffer); |
| 100 | } | ||
| 101 | |||
| 102 | /** | ||
| 103 | * Converts seconds since UTC 0 into something readable | ||
| 104 | */ | ||
| 105 | 1649 | string StringifyTime(const time_t seconds, const bool utc) { | |
| 106 | struct tm timestamp; | ||
| 107 |
2/2✓ Branch 0 taken 883 times.
✓ Branch 1 taken 766 times.
|
1649 | if (utc) { |
| 108 | 883 | localtime_r(&seconds, ×tamp); | |
| 109 | } else { | ||
| 110 | 766 | gmtime_r(&seconds, ×tamp); | |
| 111 | } | ||
| 112 | |||
| 113 | 1649 | const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
| 114 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; | ||
| 115 | char buffer[21]; | ||
| 116 | 1649 | snprintf(buffer, sizeof(buffer), "%d %s %d %02d:%02d:%02d", timestamp.tm_mday, | |
| 117 | 1649 | 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 1649 times.
✗ Branch 3 not taken.
|
1649 | return string(buffer); |
| 121 | } | ||
| 122 | |||
| 123 | /** | ||
| 124 | * Converts seconds since UTC 0 into something like 12 Sep 14:59:37 CDT | ||
| 125 | */ | ||
| 126 | 564 | string StringifyLocalTime(const time_t seconds) { | |
| 127 | struct tm timestamp; | ||
| 128 | 564 | localtime_r(&seconds, ×tamp); | |
| 129 | |||
| 130 | 564 | const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
| 131 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; | ||
| 132 | char buffer[26]; | ||
| 133 | (void)/* cast to void ignores return and placates clang-tidy */ | ||
| 134 | 564 | snprintf(buffer, sizeof(buffer), "%d %s %d %02d:%02d:%02d %s", | |
| 135 | 564 | timestamp.tm_mday, months[timestamp.tm_mon], | |
| 136 | 564 | timestamp.tm_year + 1900, timestamp.tm_hour, timestamp.tm_min, | |
| 137 | timestamp.tm_sec, timestamp.tm_zone); | ||
| 138 | |||
| 139 |
1/2✓ Branch 2 taken 564 times.
✗ Branch 3 not taken.
|
564 | return string(buffer); |
| 140 | } | ||
| 141 | |||
| 142 | |||
| 143 | /** | ||
| 144 | * Current time in format Wed, 01 Mar 2006 12:00:00 GMT | ||
| 145 | */ | ||
| 146 | 22071 | std::string RfcTimestamp() { | |
| 147 | 22071 | const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
| 148 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; | ||
| 149 | 22071 | const char *day_of_week[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; | |
| 150 | |||
| 151 | struct tm timestamp; | ||
| 152 | 22071 | const time_t now = time(NULL); | |
| 153 | 22071 | gmtime_r(&now, ×tamp); | |
| 154 | |||
| 155 | char buffer[30]; | ||
| 156 | 22071 | snprintf(buffer, sizeof(buffer), "%s, %02d %s %d %02d:%02d:%02d %s", | |
| 157 | 22071 | day_of_week[timestamp.tm_wday], timestamp.tm_mday, | |
| 158 | 22071 | months[timestamp.tm_mon], timestamp.tm_year + 1900, | |
| 159 | timestamp.tm_hour, timestamp.tm_min, timestamp.tm_sec, | ||
| 160 | timestamp.tm_zone); | ||
| 161 |
1/2✓ Branch 2 taken 22071 times.
✗ Branch 3 not taken.
|
22071 | return string(buffer); |
| 162 | } | ||
| 163 | |||
| 164 | |||
| 165 | /** | ||
| 166 | * Current time in format YYYYMMDDTHHMMSSZ. Used in AWS4 requests. | ||
| 167 | */ | ||
| 168 | 43 | std::string IsoTimestamp() { | |
| 169 | struct tm timestamp; | ||
| 170 | 43 | const time_t now = time(NULL); | |
| 171 | 43 | gmtime_r(&now, ×tamp); | |
| 172 | |||
| 173 | char buffer[17]; | ||
| 174 | 43 | snprintf(buffer, sizeof(buffer), "%04d%02d%02dT%02d%02d%02dZ", | |
| 175 | 43 | timestamp.tm_year + 1900, timestamp.tm_mon + 1, timestamp.tm_mday, | |
| 176 | timestamp.tm_hour, timestamp.tm_min, timestamp.tm_sec); | ||
| 177 |
1/2✓ Branch 2 taken 43 times.
✗ Branch 3 not taken.
|
43 | return string(buffer); |
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | /** | ||
| 182 | * UTC time in format YYYYMMDDHHMMSS. Used in cvmfs whitelists. | ||
| 183 | */ | ||
| 184 | 191 | std::string WhitelistTimestamp(time_t when) { | |
| 185 | struct tm timestamp; | ||
| 186 | 191 | gmtime_r(&when, ×tamp); | |
| 187 | |||
| 188 | char buffer[15]; | ||
| 189 | 191 | snprintf(buffer, sizeof(buffer), "%04d%02d%02d%02d%02d%02d", | |
| 190 | 191 | timestamp.tm_year + 1900, timestamp.tm_mon + 1, timestamp.tm_mday, | |
| 191 | timestamp.tm_hour, timestamp.tm_min, timestamp.tm_sec); | ||
| 192 |
1/2✓ Branch 2 taken 191 times.
✗ Branch 3 not taken.
|
191 | return string(buffer); |
| 193 | } | ||
| 194 | |||
| 195 | |||
| 196 | 17886285 | string StringifyTimeval(const timeval value) { | |
| 197 | char buffer[64]; | ||
| 198 | 17886285 | int64_t msec = value.tv_sec * 1000; | |
| 199 | 17886285 | msec += value.tv_usec / 1000; | |
| 200 | 17886285 | snprintf(buffer, sizeof(buffer), "%" PRId64 ".%03d", msec, | |
| 201 | 17886285 | static_cast<int>(value.tv_usec % 1000)); | |
| 202 |
1/2✓ Branch 2 taken 17886285 times.
✗ Branch 3 not taken.
|
17886285 | return string(buffer); |
| 203 | } | ||
| 204 | |||
| 205 | /** | ||
| 206 | * Parses a timestamp of the form YYYY-MM-DDTHH:MM:SSZ | ||
| 207 | * Return 0 on error | ||
| 208 | */ | ||
| 209 | 3175 | time_t IsoTimestamp2UtcTime(const std::string &iso8601) { | |
| 210 | 3175 | time_t utc_time = 0; | |
| 211 | 3175 | const unsigned length = iso8601.length(); | |
| 212 | |||
| 213 |
2/2✓ Branch 0 taken 76 times.
✓ Branch 1 taken 3099 times.
|
3175 | if (length != 20) |
| 214 | 76 | return utc_time; | |
| 215 |
2/4✓ Branch 2 taken 3099 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 3099 times.
✗ Branch 6 not taken.
|
6198 | if ((iso8601[4] != '-') || (iso8601[7] != '-') || (iso8601[10] != 'T') |
| 216 |
5/10✓ Branch 0 taken 3099 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 3099 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 3099 times.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 3099 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 3099 times.
|
6198 | || (iso8601[13] != ':') || (iso8601[16] != ':') || (iso8601[19] != 'Z')) { |
| 217 | ✗ | return utc_time; | |
| 218 | } | ||
| 219 | |||
| 220 | struct tm tm_wl; | ||
| 221 | 3099 | memset(&tm_wl, 0, sizeof(struct tm)); | |
| 222 |
2/4✓ Branch 1 taken 3099 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3099 times.
✗ Branch 5 not taken.
|
3099 | tm_wl.tm_year = static_cast<int>(String2Int64(iso8601.substr(0, 4))) - 1900; |
| 223 |
2/4✓ Branch 1 taken 3099 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3099 times.
✗ Branch 5 not taken.
|
3099 | tm_wl.tm_mon = static_cast<int>(String2Int64(iso8601.substr(5, 2))) - 1; |
| 224 |
2/4✓ Branch 1 taken 3099 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3099 times.
✗ Branch 5 not taken.
|
3099 | tm_wl.tm_mday = static_cast<int>(String2Int64(iso8601.substr(8, 2))); |
| 225 |
2/4✓ Branch 1 taken 3099 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3099 times.
✗ Branch 5 not taken.
|
3099 | tm_wl.tm_hour = static_cast<int>(String2Int64(iso8601.substr(11, 2))); |
| 226 |
2/4✓ Branch 1 taken 3099 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3099 times.
✗ Branch 5 not taken.
|
3099 | tm_wl.tm_min = static_cast<int>(String2Int64(iso8601.substr(14, 2))); |
| 227 |
2/4✓ Branch 1 taken 3099 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3099 times.
✗ Branch 5 not taken.
|
3099 | tm_wl.tm_sec = static_cast<int>(String2Int64(iso8601.substr(17, 2))); |
| 228 | 3099 | utc_time = timegm(&tm_wl); | |
| 229 |
2/2✓ Branch 0 taken 38 times.
✓ Branch 1 taken 3061 times.
|
3099 | if (utc_time < 0) |
| 230 | 38 | return 0; | |
| 231 | |||
| 232 | 3061 | return utc_time; | |
| 233 | } | ||
| 234 | |||
| 235 | 32277 | int64_t String2Int64(const string &value) { | |
| 236 | int64_t result; | ||
| 237 | 32277 | sscanf(value.c_str(), "%" PRId64, &result); | |
| 238 | 32277 | return result; | |
| 239 | } | ||
| 240 | |||
| 241 | 85425 | uint64_t String2Uint64(const string &value) { | |
| 242 | uint64_t result; | ||
| 243 |
2/2✓ Branch 1 taken 85365 times.
✓ Branch 2 taken 60 times.
|
85425 | if (sscanf(value.c_str(), "%" PRIu64, &result) == 1) { |
| 244 | 85365 | return result; | |
| 245 | } | ||
| 246 | 60 | return 0; | |
| 247 | } | ||
| 248 | |||
| 249 | /** | ||
| 250 | * Parse a string into a a uint64_t. | ||
| 251 | * | ||
| 252 | * Unlike String2Uint64, this: | ||
| 253 | * - Checks to make sure the full string is parsed | ||
| 254 | * - Can indicate an error occurred. | ||
| 255 | * | ||
| 256 | * If an error occurs, this returns false and sets errno appropriately. | ||
| 257 | */ | ||
| 258 | 4129 | bool String2Uint64Parse(const std::string &value, uint64_t *result) { | |
| 259 | 4129 | char *endptr = NULL; | |
| 260 | 4129 | errno = 0; | |
| 261 | 4129 | long long myval = strtoll(value.c_str(), &endptr, 10); // NOLINT | |
| 262 |
2/2✓ Branch 3 taken 3366 times.
✓ Branch 4 taken 660 times.
|
8155 | if ((value.size() == 0) || (endptr != (value.c_str() + value.size())) |
| 263 |
6/6✓ Branch 0 taken 4026 times.
✓ Branch 1 taken 103 times.
✓ Branch 2 taken 31 times.
✓ Branch 3 taken 3335 times.
✓ Branch 4 taken 794 times.
✓ Branch 5 taken 3335 times.
|
8155 | || (myval < 0)) { |
| 264 | 794 | errno = EINVAL; | |
| 265 | 794 | return false; | |
| 266 | } | ||
| 267 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3335 times.
|
3335 | if (errno) { |
| 268 | ✗ | return false; | |
| 269 | } | ||
| 270 |
2/2✓ Branch 0 taken 3304 times.
✓ Branch 1 taken 31 times.
|
3335 | if (result) { |
| 271 | 3304 | *result = myval; | |
| 272 | } | ||
| 273 | 3335 | return true; | |
| 274 | } | ||
| 275 | |||
| 276 | 172 | void String2Uint64Pair(const string &value, uint64_t *a, uint64_t *b) { | |
| 277 | 172 | sscanf(value.c_str(), "%" PRIu64 " %" PRIu64, a, b); | |
| 278 | 172 | } | |
| 279 | |||
| 280 | 164858 | bool HasPrefix(const string &str, const string &prefix, | |
| 281 | const bool ignore_case) { | ||
| 282 |
2/2✓ Branch 2 taken 28977 times.
✓ Branch 3 taken 135881 times.
|
164858 | if (prefix.length() > str.length()) |
| 283 | 28977 | return false; | |
| 284 | |||
| 285 |
2/2✓ Branch 1 taken 514469 times.
✓ Branch 2 taken 42527 times.
|
556996 | for (unsigned i = 0, l = prefix.length(); i < l; ++i) { |
| 286 |
2/2✓ Branch 0 taken 92235 times.
✓ Branch 1 taken 422234 times.
|
514469 | if (ignore_case) { |
| 287 |
2/2✓ Branch 2 taken 52442 times.
✓ Branch 3 taken 39793 times.
|
92235 | if (toupper(str[i]) != toupper(prefix[i])) |
| 288 | 52442 | return false; | |
| 289 | } else { | ||
| 290 |
2/2✓ Branch 2 taken 40912 times.
✓ Branch 3 taken 381322 times.
|
422234 | if (str[i] != prefix[i]) |
| 291 | 40912 | return false; | |
| 292 | } | ||
| 293 | } | ||
| 294 | 42527 | return true; | |
| 295 | } | ||
| 296 | |||
| 297 | 13157 | bool HasSuffix(const std::string &str, const std::string &suffix, | |
| 298 | const bool ignore_case) { | ||
| 299 |
2/2✓ Branch 2 taken 212 times.
✓ Branch 3 taken 12945 times.
|
13157 | if (suffix.size() > str.size()) |
| 300 | 212 | return false; | |
| 301 | 12945 | const IgnoreCaseComperator icmp; | |
| 302 | return (ignore_case) | ||
| 303 |
3/4✓ Branch 0 taken 277 times.
✓ Branch 1 taken 12668 times.
✓ Branch 6 taken 277 times.
✗ Branch 7 not taken.
|
12945 | ? std::equal(suffix.rbegin(), suffix.rend(), str.rbegin(), icmp) |
| 304 |
1/2✓ Branch 4 taken 12668 times.
✗ Branch 5 not taken.
|
12945 | : std::equal(suffix.rbegin(), suffix.rend(), str.rbegin()); |
| 305 | } | ||
| 306 | |||
| 307 | 37745 | vector<string> SplitString(const string &str, char delim) { | |
| 308 | 37745 | return SplitStringBounded(0, str, delim); | |
| 309 | } | ||
| 310 | |||
| 311 | 38003 | vector<string> SplitStringBounded(unsigned max_chunks, const string &str, | |
| 312 | char delim) { | ||
| 313 | 38003 | vector<string> result; | |
| 314 | |||
| 315 | // edge case... one chunk is always the whole string | ||
| 316 |
2/2✓ Branch 0 taken 43 times.
✓ Branch 1 taken 37960 times.
|
38003 | if (1 == max_chunks) { |
| 317 |
1/2✓ Branch 1 taken 43 times.
✗ Branch 2 not taken.
|
43 | result.push_back(str); |
| 318 | 43 | return result; | |
| 319 | } | ||
| 320 | |||
| 321 | // split the string | ||
| 322 | 37960 | const unsigned size = str.size(); | |
| 323 | 37960 | unsigned marker = 0; | |
| 324 | 37960 | unsigned chunks = 1; | |
| 325 | unsigned i; | ||
| 326 |
2/2✓ Branch 0 taken 848399 times.
✓ Branch 1 taken 37874 times.
|
886273 | for (i = 0; i < size; ++i) { |
| 327 |
2/2✓ Branch 1 taken 57485 times.
✓ Branch 2 taken 790914 times.
|
848399 | if (str[i] == delim) { |
| 328 |
2/4✓ Branch 1 taken 57485 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 57485 times.
✗ Branch 5 not taken.
|
57485 | result.push_back(str.substr(marker, i - marker)); |
| 329 | 57485 | marker = i + 1; | |
| 330 | |||
| 331 | // we got what we want... good bye | ||
| 332 |
2/2✓ Branch 0 taken 86 times.
✓ Branch 1 taken 57399 times.
|
57485 | if (++chunks == max_chunks) |
| 333 | 86 | break; | |
| 334 | } | ||
| 335 | } | ||
| 336 | |||
| 337 | // push the remainings of the string and return | ||
| 338 |
2/4✓ Branch 1 taken 37960 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 37960 times.
✗ Branch 5 not taken.
|
37960 | result.push_back(str.substr(marker)); |
| 339 | 37960 | return result; | |
| 340 | } | ||
| 341 | |||
| 342 | 135 | vector<string> SplitStringMultiChar(const string &str, const string &delim) { | |
| 343 | 135 | size_t pos_start = 0, pos_end = 0, delim_len = delim.length(); | |
| 344 | 135 | std::string substring; | |
| 345 | 135 | std::vector<std::string> result; | |
| 346 | |||
| 347 |
2/2✓ Branch 1 taken 475 times.
✓ Branch 2 taken 135 times.
|
610 | while ((pos_end = str.find(delim, pos_start)) != string::npos) { |
| 348 |
1/2✓ Branch 1 taken 475 times.
✗ Branch 2 not taken.
|
475 | substring = str.substr(pos_start, pos_end - pos_start); |
| 349 | 475 | pos_start = pos_end + delim_len; | |
| 350 |
1/2✓ Branch 1 taken 475 times.
✗ Branch 2 not taken.
|
475 | result.push_back(substring); |
| 351 | } | ||
| 352 | |||
| 353 |
2/4✓ Branch 1 taken 135 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 135 times.
✗ Branch 5 not taken.
|
135 | result.push_back(str.substr(pos_start)); |
| 354 | 270 | return result; | |
| 355 | 135 | } | |
| 356 | |||
| 357 | 7218 | string JoinStrings(const vector<string> &strings, const string &joint) { | |
| 358 |
1/2✓ Branch 2 taken 7218 times.
✗ Branch 3 not taken.
|
7218 | string result = ""; |
| 359 | 7218 | const unsigned size = strings.size(); | |
| 360 | |||
| 361 |
2/2✓ Branch 0 taken 5179 times.
✓ Branch 1 taken 2039 times.
|
7218 | if (size > 0) { |
| 362 |
1/2✓ Branch 2 taken 5179 times.
✗ Branch 3 not taken.
|
5179 | result = strings[0]; |
| 363 |
2/2✓ Branch 0 taken 698 times.
✓ Branch 1 taken 5179 times.
|
5877 | for (unsigned i = 1; i < size; ++i) |
| 364 |
2/4✓ Branch 2 taken 698 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 698 times.
✗ Branch 6 not taken.
|
698 | result += joint + strings[i]; |
| 365 | } | ||
| 366 | |||
| 367 | 7218 | return result; | |
| 368 | } | ||
| 369 | |||
| 370 | 958 | void ParseKeyvalMem(const unsigned char *buffer, const unsigned buffer_size, | |
| 371 | map<char, string> *content) { | ||
| 372 | 958 | string line; | |
| 373 | 958 | unsigned pos = 0; | |
| 374 |
2/2✓ Branch 0 taken 160729 times.
✓ Branch 1 taken 20 times.
|
160749 | while (pos < buffer_size) { |
| 375 |
2/2✓ Branch 0 taken 9631 times.
✓ Branch 1 taken 151098 times.
|
160729 | if (static_cast<char>(buffer[pos]) == '\n') { |
| 376 |
2/2✓ Branch 1 taken 938 times.
✓ Branch 2 taken 8693 times.
|
9631 | if (line == "--") |
| 377 | 938 | return; | |
| 378 | |||
| 379 |
1/2✓ Branch 1 taken 8693 times.
✗ Branch 2 not taken.
|
8693 | if (line != "") { |
| 380 |
3/11✗ Branch 1 not taken.
✓ Branch 2 taken 8693 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 8693 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 8693 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
|
8693 | const string tail = (line.length() == 1) ? "" : line.substr(1); |
| 381 | // Special handling of 'Z' key because it can exist multiple times | ||
| 382 |
3/4✓ Branch 1 taken 8693 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8547 times.
✓ Branch 4 taken 146 times.
|
8693 | if (line[0] != 'Z') { |
| 383 |
3/6✓ Branch 1 taken 8547 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 8547 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 8547 times.
✗ Branch 8 not taken.
|
8547 | (*content)[line[0]] = tail; |
| 384 | } else { | ||
| 385 |
4/7✓ Branch 2 taken 146 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 146 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 73 times.
✓ Branch 9 taken 73 times.
|
146 | if (content->find(line[0]) == content->end()) { |
| 386 |
3/6✓ Branch 1 taken 73 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 73 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 73 times.
✗ Branch 8 not taken.
|
73 | (*content)[line[0]] = tail; |
| 387 | } else { | ||
| 388 |
6/12✓ Branch 1 taken 73 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 73 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 73 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 73 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 73 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 73 times.
✗ Branch 17 not taken.
|
73 | (*content)[line[0]] = (*content)[line[0]] + "|" + tail; |
| 389 | } | ||
| 390 | } | ||
| 391 | 8693 | } | |
| 392 |
1/2✓ Branch 1 taken 8693 times.
✗ Branch 2 not taken.
|
8693 | line = ""; |
| 393 | } else { | ||
| 394 |
1/2✓ Branch 1 taken 151098 times.
✗ Branch 2 not taken.
|
151098 | line += static_cast<char>(buffer[pos]); |
| 395 | } | ||
| 396 | 159791 | pos++; | |
| 397 | } | ||
| 398 |
2/2✓ Branch 1 taken 20 times.
✓ Branch 2 taken 938 times.
|
958 | } |
| 399 | |||
| 400 | 244 | bool ParseKeyvalPath(const string &filename, map<char, string> *content) { | |
| 401 |
1/2✓ Branch 2 taken 244 times.
✗ Branch 3 not taken.
|
244 | const int fd = open(filename.c_str(), O_RDONLY); |
| 402 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 209 times.
|
244 | if (fd < 0) |
| 403 | 35 | return false; | |
| 404 | |||
| 405 | unsigned char buffer[4096]; | ||
| 406 |
1/2✓ Branch 1 taken 209 times.
✗ Branch 2 not taken.
|
209 | const ssize_t num_bytes = read(fd, buffer, sizeof(buffer)); |
| 407 |
1/2✓ Branch 1 taken 209 times.
✗ Branch 2 not taken.
|
209 | close(fd); |
| 408 | |||
| 409 |
3/4✓ Branch 0 taken 174 times.
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 174 times.
|
209 | if ((num_bytes <= 0) || (unsigned(num_bytes) >= sizeof(buffer))) |
| 410 | 35 | return false; | |
| 411 | |||
| 412 |
1/2✓ Branch 1 taken 174 times.
✗ Branch 2 not taken.
|
174 | ParseKeyvalMem(buffer, unsigned(num_bytes), content); |
| 413 | 174 | return true; | |
| 414 | } | ||
| 415 | |||
| 416 | 20723 | string GetLineMem(const char *text, const int text_size) { | |
| 417 | 20723 | int pos = 0; | |
| 418 |
4/4✓ Branch 0 taken 761187 times.
✓ Branch 1 taken 695 times.
✓ Branch 2 taken 741159 times.
✓ Branch 3 taken 20028 times.
|
761882 | while ((pos < text_size) && (text[pos] != '\n')) |
| 419 | 741159 | pos++; | |
| 420 |
1/2✓ Branch 2 taken 20723 times.
✗ Branch 3 not taken.
|
20723 | return string(text, pos); |
| 421 | } | ||
| 422 | |||
| 423 | 17909075 | bool GetLineFile(FILE *f, std::string *line) { | |
| 424 | int retval; | ||
| 425 | 17909075 | line->clear(); | |
| 426 | while (true) { | ||
| 427 | 1277444872 | retval = fgetc(f); | |
| 428 |
2/6✗ Branch 1 not taken.
✓ Branch 2 taken 1277444872 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1277444872 times.
|
1277444872 | if (ferror(f) && (errno == EINTR)) { |
| 429 | ✗ | clearerr(f); | |
| 430 | ✗ | continue; | |
| 431 |
2/2✓ Branch 0 taken 4823 times.
✓ Branch 1 taken 1277440049 times.
|
1277444872 | } else if (retval == EOF) { |
| 432 | 4823 | break; | |
| 433 | } | ||
| 434 | 1277440049 | const char c = static_cast<char>(retval); | |
| 435 |
2/2✓ Branch 0 taken 17904252 times.
✓ Branch 1 taken 1259535797 times.
|
1277440049 | if (c == '\n') |
| 436 | 17904252 | break; | |
| 437 | 1259535797 | line->push_back(c); | |
| 438 | 1259535797 | } | |
| 439 |
4/4✓ Branch 0 taken 4823 times.
✓ Branch 1 taken 17904252 times.
✓ Branch 3 taken 669 times.
✓ Branch 4 taken 4154 times.
|
17909075 | return (retval != EOF) || !line->empty(); |
| 440 | } | ||
| 441 | |||
| 442 | 3398 | bool GetLineFd(const int fd, std::string *line) { | |
| 443 | ssize_t retval; | ||
| 444 | char c; | ||
| 445 | 3398 | line->clear(); | |
| 446 | while (true) { | ||
| 447 |
1/2✓ Branch 1 taken 147688 times.
✗ Branch 2 not taken.
|
147688 | retval = read(fd, &c, 1); |
| 448 |
2/2✓ Branch 0 taken 93 times.
✓ Branch 1 taken 147595 times.
|
147688 | if (retval == 0) { |
| 449 | 93 | break; | |
| 450 | } | ||
| 451 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 147595 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
147595 | if ((retval == -1) && (errno == EINTR)) { |
| 452 | ✗ | continue; | |
| 453 | } | ||
| 454 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 147595 times.
|
147595 | if (retval == -1) { |
| 455 | ✗ | break; | |
| 456 | } | ||
| 457 |
2/2✓ Branch 0 taken 3305 times.
✓ Branch 1 taken 144290 times.
|
147595 | if (c == '\n') |
| 458 | 3305 | break; | |
| 459 |
1/2✓ Branch 1 taken 144290 times.
✗ Branch 2 not taken.
|
144290 | line->push_back(c); |
| 460 | } | ||
| 461 |
4/4✓ Branch 0 taken 93 times.
✓ Branch 1 taken 3305 times.
✓ Branch 3 taken 49 times.
✓ Branch 4 taken 44 times.
|
3398 | return (retval == 1) || !line->empty(); |
| 462 | } | ||
| 463 | |||
| 464 | /** | ||
| 465 | * Removes leading and trailing whitespaces. | ||
| 466 | */ | ||
| 467 | 16976 | string Trim(const string &raw, bool trim_newline) { | |
| 468 |
2/2✓ Branch 1 taken 1397 times.
✓ Branch 2 taken 15579 times.
|
16976 | if (raw.empty()) |
| 469 |
1/2✓ Branch 2 taken 1397 times.
✗ Branch 3 not taken.
|
1397 | return ""; |
| 470 | |||
| 471 | 15579 | unsigned start_pos = 0; | |
| 472 | 17740 | for (; (start_pos < raw.length()) | |
| 473 |
7/8✓ Branch 0 taken 17584 times.
✓ Branch 1 taken 156 times.
✓ Branch 3 taken 15778 times.
✓ Branch 4 taken 1806 times.
✓ Branch 6 taken 15778 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2161 times.
✓ Branch 9 taken 15579 times.
|
33518 | && (raw[start_pos] == ' ' || raw[start_pos] == '\t' |
| 474 |
2/2✓ Branch 0 taken 1870 times.
✓ Branch 1 taken 13908 times.
|
15778 | || (trim_newline |
| 475 |
4/4✓ Branch 1 taken 1714 times.
✓ Branch 2 taken 156 times.
✓ Branch 4 taken 199 times.
✓ Branch 5 taken 1515 times.
|
1870 | && (raw[start_pos] == '\n' || raw[start_pos] == '\r'))); |
| 476 | ++start_pos) { | ||
| 477 | } | ||
| 478 | 15579 | unsigned end_pos = raw.length() - 1; // at least one character in raw | |
| 479 | 19293 | for (; | |
| 480 | (end_pos >= start_pos) | ||
| 481 |
7/8✓ Branch 0 taken 19137 times.
✓ Branch 1 taken 156 times.
✓ Branch 3 taken 17732 times.
✓ Branch 4 taken 1405 times.
✓ Branch 6 taken 17732 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3714 times.
✓ Branch 9 taken 15579 times.
|
37025 | && (raw[end_pos] == ' ' || raw[end_pos] == '\t' |
| 482 |
6/6✓ Branch 0 taken 3824 times.
✓ Branch 1 taken 13908 times.
✓ Branch 3 taken 2369 times.
✓ Branch 4 taken 1455 times.
✓ Branch 6 taken 854 times.
✓ Branch 7 taken 1515 times.
|
17732 | || (trim_newline && (raw[end_pos] == '\n' || raw[end_pos] == '\r'))); |
| 483 | --end_pos) { | ||
| 484 | } | ||
| 485 | |||
| 486 | 15579 | return raw.substr(start_pos, end_pos - start_pos + 1); | |
| 487 | } | ||
| 488 | |||
| 489 | 56 | std::string TrimString(const std::string &path, | |
| 490 | const std::string &toTrim, | ||
| 491 | const int trimMode) { | ||
| 492 | 56 | std::string trimmed = path; | |
| 493 |
1/2✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
|
56 | if (trimmed != toTrim) { |
| 494 |
3/4✓ Branch 1 taken 112 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 84 times.
✓ Branch 4 taken 28 times.
|
168 | while ((trimMode & kTrimLeading) && HasPrefix(trimmed, toTrim, true) |
| 495 |
5/6✓ Branch 0 taken 112 times.
✓ Branch 1 taken 28 times.
✓ Branch 4 taken 84 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 84 times.
✓ Branch 7 taken 56 times.
|
252 | && (trimmed.size() > toTrim.size())) { |
| 496 |
1/2✓ Branch 2 taken 84 times.
✗ Branch 3 not taken.
|
84 | trimmed = trimmed.substr(toTrim.size()); |
| 497 | } | ||
| 498 |
3/4✓ Branch 1 taken 84 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 56 times.
✓ Branch 4 taken 28 times.
|
140 | while ((trimMode & kTrimTrailing) && HasSuffix(trimmed, toTrim, true) |
| 499 |
5/6✓ Branch 0 taken 84 times.
✓ Branch 1 taken 28 times.
✓ Branch 4 taken 56 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 56 times.
✓ Branch 7 taken 56 times.
|
196 | && (trimmed.size() > toTrim.size())) { |
| 500 |
1/2✓ Branch 3 taken 56 times.
✗ Branch 4 not taken.
|
56 | trimmed = trimmed.substr(0, trimmed.size() - toTrim.size()); |
| 501 | } | ||
| 502 | } | ||
| 503 | 56 | return trimmed; | |
| 504 | } | ||
| 505 | |||
| 506 | /** | ||
| 507 | * Converts all characters to upper case | ||
| 508 | */ | ||
| 509 | 2705 | string ToUpper(const string &mixed_case) { | |
| 510 | 2705 | string result(mixed_case); | |
| 511 |
2/2✓ Branch 1 taken 7008 times.
✓ Branch 2 taken 2705 times.
|
9713 | for (unsigned i = 0, l = result.length(); i < l; ++i) { |
| 512 |
2/4✓ Branch 1 taken 7008 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 7008 times.
✗ Branch 5 not taken.
|
7008 | result[i] = static_cast<char>(toupper(result[i])); |
| 513 | } | ||
| 514 | 2705 | return result; | |
| 515 | } | ||
| 516 | |||
| 517 | 4102 | string ReplaceAll(const string &haystack, const string &needle, | |
| 518 | const string &replace_by) { | ||
| 519 | 4102 | string result(haystack); | |
| 520 | 4102 | size_t pos = 0; | |
| 521 | 4102 | const unsigned needle_size = needle.size(); | |
| 522 |
2/2✓ Branch 1 taken 35 times.
✓ Branch 2 taken 4067 times.
|
4102 | if (needle == "") |
| 523 | 35 | return result; | |
| 524 | |||
| 525 |
2/2✓ Branch 1 taken 2608 times.
✓ Branch 2 taken 4067 times.
|
6675 | while ((pos = result.find(needle, pos)) != string::npos) |
| 526 |
1/2✓ Branch 1 taken 2608 times.
✗ Branch 2 not taken.
|
2608 | result.replace(pos, needle_size, replace_by); |
| 527 | 4067 | return result; | |
| 528 | } | ||
| 529 | |||
| 530 | 341355 | static inline void Base64Block(const unsigned char input[3], const char *table, | |
| 531 | char output[4]) { | ||
| 532 | 341355 | output[0] = table[(input[0] & 0xFD) >> 2]; | |
| 533 | 341355 | output[1] = table[((input[0] & 0x03) << 4) | ((input[1] & 0xF0) >> 4)]; | |
| 534 | 341355 | output[2] = table[((input[1] & 0x0F) << 2) | ((input[2] & 0xD0) >> 6)]; | |
| 535 | 341355 | output[3] = table[input[2] & 0x3F]; | |
| 536 | 341355 | } | |
| 537 | |||
| 538 | 28684 | string Base64(const string &data) { | |
| 539 | 28684 | string result; | |
| 540 |
1/2✓ Branch 2 taken 28684 times.
✗ Branch 3 not taken.
|
28684 | result.reserve((data.length() + 3) * 4 / 3); |
| 541 | 28684 | unsigned pos = 0; | |
| 542 | const unsigned char *data_ptr = reinterpret_cast<const unsigned char *>( | ||
| 543 | 28684 | data.data()); | |
| 544 | 28684 | const unsigned length = data.length(); | |
| 545 |
2/2✓ Branch 0 taken 313100 times.
✓ Branch 1 taken 28684 times.
|
341784 | while (pos + 2 < length) { |
| 546 | char encoded_block[4]; | ||
| 547 | 313100 | Base64Block(data_ptr + pos, b64_table, encoded_block); | |
| 548 |
1/2✓ Branch 1 taken 313100 times.
✗ Branch 2 not taken.
|
313100 | result.append(encoded_block, 4); |
| 549 | 313100 | pos += 3; | |
| 550 | } | ||
| 551 |
2/2✓ Branch 0 taken 28255 times.
✓ Branch 1 taken 429 times.
|
28684 | if (length % 3 != 0) { |
| 552 | unsigned char input[3]; | ||
| 553 | 28255 | input[0] = data_ptr[pos]; | |
| 554 |
2/2✓ Branch 0 taken 18894 times.
✓ Branch 1 taken 9361 times.
|
28255 | input[1] = ((length % 3) == 2) ? data_ptr[pos + 1] : 0; |
| 555 | 28255 | input[2] = 0; | |
| 556 | char encoded_block[4]; | ||
| 557 | 28255 | Base64Block(input, b64_table, encoded_block); | |
| 558 |
1/2✓ Branch 1 taken 28255 times.
✗ Branch 2 not taken.
|
28255 | result.append(encoded_block, 2); |
| 559 |
3/4✓ Branch 0 taken 18894 times.
✓ Branch 1 taken 9361 times.
✓ Branch 3 taken 28255 times.
✗ Branch 4 not taken.
|
28255 | result.push_back(((length % 3) == 2) ? encoded_block[2] : '='); |
| 560 |
1/2✓ Branch 1 taken 28255 times.
✗ Branch 2 not taken.
|
28255 | result.push_back('='); |
| 561 | } | ||
| 562 | |||
| 563 | 28684 | return result; | |
| 564 | } | ||
| 565 | |||
| 566 | /** | ||
| 567 | * Safe encoding for URIs and path names: replace + by - and / by _ | ||
| 568 | */ | ||
| 569 | 35 | string Base64Url(const string &data) { | |
| 570 | 35 | string base64 = Base64(data); | |
| 571 |
2/2✓ Branch 1 taken 9340 times.
✓ Branch 2 taken 35 times.
|
9375 | for (unsigned i = 0, l = base64.length(); i < l; ++i) { |
| 572 |
3/4✓ Branch 1 taken 9340 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 162 times.
✓ Branch 4 taken 9178 times.
|
9340 | if (base64[i] == '+') { |
| 573 |
1/2✓ Branch 1 taken 162 times.
✗ Branch 2 not taken.
|
162 | base64[i] = '-'; |
| 574 |
3/4✓ Branch 1 taken 9178 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 108 times.
✓ Branch 4 taken 9070 times.
|
9178 | } else if (base64[i] == '/') { |
| 575 |
1/2✓ Branch 1 taken 108 times.
✗ Branch 2 not taken.
|
108 | base64[i] = '_'; |
| 576 | } | ||
| 577 | } | ||
| 578 | 35 | return base64; | |
| 579 | } | ||
| 580 | |||
| 581 | 461407 | static bool Debase64Block(const unsigned char input[4], | |
| 582 | unsigned char output[3]) { | ||
| 583 | int32_t dec[4]; | ||
| 584 |
2/2✓ Branch 0 taken 1845547 times.
✓ Branch 1 taken 461380 times.
|
2306927 | for (int i = 0; i < 4; ++i) { |
| 585 | 1845547 | dec[i] = db64_table[input[i]]; | |
| 586 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 1845520 times.
|
1845547 | if (dec[i] < 0) |
| 587 | 27 | return false; | |
| 588 | } | ||
| 589 | |||
| 590 | 461380 | output[0] = (dec[0] << 2) | (dec[1] >> 4); | |
| 591 | 461380 | output[1] = ((dec[1] & 0x0F) << 4) | (dec[2] >> 2); | |
| 592 | 461380 | output[2] = ((dec[2] & 0x03) << 6) | dec[3]; | |
| 593 | 461380 | return true; | |
| 594 | } | ||
| 595 | |||
| 596 | /** | ||
| 597 | * Can decode both base64 and base64url | ||
| 598 | */ | ||
| 599 | 2343 | bool Debase64(const string &data, string *decoded) { | |
| 600 | 2343 | decoded->clear(); | |
| 601 | 2343 | decoded->reserve((data.length() + 4) * 3 / 4); | |
| 602 | 2343 | unsigned pos = 0; | |
| 603 | const unsigned char *data_ptr = reinterpret_cast<const unsigned char *>( | ||
| 604 | 2343 | data.data()); | |
| 605 | 2343 | const unsigned length = data.length(); | |
| 606 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 2316 times.
|
2343 | if (length == 0) |
| 607 | 27 | return true; | |
| 608 |
2/2✓ Branch 0 taken 54 times.
✓ Branch 1 taken 2262 times.
|
2316 | if ((length % 4) != 0) |
| 609 | 54 | return false; | |
| 610 | |||
| 611 |
2/2✓ Branch 0 taken 461407 times.
✓ Branch 1 taken 2235 times.
|
463642 | while (pos < length) { |
| 612 | unsigned char decoded_block[3]; | ||
| 613 | 461407 | const bool retval = Debase64Block(data_ptr + pos, decoded_block); | |
| 614 |
2/2✓ Branch 0 taken 27 times.
✓ Branch 1 taken 461380 times.
|
461407 | if (!retval) |
| 615 | 27 | return false; | |
| 616 |
1/2✓ Branch 1 taken 461380 times.
✗ Branch 2 not taken.
|
461380 | decoded->append(reinterpret_cast<char *>(decoded_block), 3); |
| 617 | 461380 | pos += 4; | |
| 618 | } | ||
| 619 | |||
| 620 |
2/2✓ Branch 0 taken 4470 times.
✓ Branch 1 taken 2235 times.
|
6705 | for (int i = 0; i < 2; ++i) { |
| 621 | 4470 | pos--; | |
| 622 |
2/2✓ Branch 1 taken 736 times.
✓ Branch 2 taken 3734 times.
|
4470 | if (data[pos] == '=') |
| 623 | 736 | decoded->erase(decoded->length() - 1); | |
| 624 | } | ||
| 625 | 2235 | return true; | |
| 626 | } | ||
| 627 | |||
| 628 | /** | ||
| 629 | * Assumes that source is terminated by a newline | ||
| 630 | */ | ||
| 631 | 245 | string Tail(const string &source, unsigned num_lines) { | |
| 632 |
6/6✓ Branch 1 taken 175 times.
✓ Branch 2 taken 70 times.
✓ Branch 3 taken 35 times.
✓ Branch 4 taken 140 times.
✓ Branch 5 taken 105 times.
✓ Branch 6 taken 140 times.
|
245 | if (source.empty() || (num_lines == 0)) |
| 633 |
1/2✓ Branch 2 taken 105 times.
✗ Branch 3 not taken.
|
105 | return ""; |
| 634 | |||
| 635 | 140 | const int l = static_cast<int>(source.length()); | |
| 636 | 140 | int i = l - 1; | |
| 637 |
2/2✓ Branch 0 taken 560 times.
✓ Branch 1 taken 105 times.
|
665 | for (; i >= 0; --i) { |
| 638 | 560 | const char c = source.data()[i]; | |
| 639 |
2/2✓ Branch 0 taken 175 times.
✓ Branch 1 taken 385 times.
|
560 | if (c == '\n') { |
| 640 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 140 times.
|
175 | if (num_lines == 0) { |
| 641 | 35 | return source.substr(i + 1); | |
| 642 | } | ||
| 643 | 140 | num_lines--; | |
| 644 | } | ||
| 645 | } | ||
| 646 | 105 | return source; | |
| 647 | } | ||
| 648 | |||
| 649 | /** | ||
| 650 | * Get UTC Time. | ||
| 651 | * | ||
| 652 | * @param format format if timestamp (YYYY-MM-DD HH:MM:SS by default) | ||
| 653 | * @return a timestamp string on success, empty string on failure | ||
| 654 | */ | ||
| 655 | ✗ | std::string GetGMTimestamp(const std::string &format) { | |
| 656 | struct tm time_ptr; | ||
| 657 | char date_and_time[100]; | ||
| 658 | ✗ | const time_t t = time(NULL); | |
| 659 | ✗ | gmtime_r(&t, &time_ptr); // take UTC | |
| 660 | // return empty string if formatting fails | ||
| 661 | ✗ | if (!strftime(date_and_time, 100, format.c_str(), &time_ptr)) { | |
| 662 | ✗ | return ""; | |
| 663 | } | ||
| 664 | ✗ | std::string timestamp(date_and_time); | |
| 665 | ✗ | return timestamp; | |
| 666 | } | ||
| 667 | |||
| 668 | #ifdef CVMFS_NAMESPACE_GUARD | ||
| 669 | } // namespace CVMFS_NAMESPACE_GUARD | ||
| 670 | #endif | ||
| 671 |