| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/util/string.cc |
| Date: | 2025-11-09 02:35:23 |
| 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 | #ifndef __STDC_FORMAT_MACROS | ||
| 8 | // NOLINTNEXTLINE | ||
| 9 | #define __STDC_FORMAT_MACROS | ||
| 10 | #endif | ||
| 11 | |||
| 12 | #include "string.h" | ||
| 13 | |||
| 14 | #include <cctype> | ||
| 15 | #include <cstdio> | ||
| 16 | #include <cstdlib> | ||
| 17 | #include <cstring> | ||
| 18 | #include <ctime> | ||
| 19 | #include <algorithm> | ||
| 20 | #include <vector> | ||
| 21 | #include <map> | ||
| 22 | #include <stdio.h> | ||
| 23 | #include <errno.h> | ||
| 24 | #include <fcntl.h> | ||
| 25 | #include <inttypes.h> | ||
| 26 | #include <stdint.h> | ||
| 27 | #include <time.h> | ||
| 28 | #include <string> | ||
| 29 | #include <unistd.h> | ||
| 30 | |||
| 31 | |||
| 32 | using namespace std; // NOLINT | ||
| 33 | |||
| 34 | #ifdef CVMFS_NAMESPACE_GUARD | ||
| 35 | namespace CVMFS_NAMESPACE_GUARD { | ||
| 36 | #endif | ||
| 37 | |||
| 38 | const char b64_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', | ||
| 39 | 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', | ||
| 40 | 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', | ||
| 41 | 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', | ||
| 42 | 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', | ||
| 43 | '3', '4', '5', '6', '7', '8', '9', '+', '/'}; | ||
| 44 | |||
| 45 | /** | ||
| 46 | * Decode Base64 and Base64Url | ||
| 47 | */ | ||
| 48 | const int8_t db64_table[] = { | ||
| 49 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
| 50 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
| 51 | -1, -1, -1, -1, -1, 62, -1, 62, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, | ||
| 52 | 61, -1, -1, -1, 0, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, | ||
| 53 | 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, | ||
| 54 | 63, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, | ||
| 55 | 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, | ||
| 56 | |||
| 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, -1, -1, -1, -1, -1, | ||
| 59 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
| 60 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
| 61 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
| 62 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
| 63 | -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
| 64 | }; | ||
| 65 | |||
| 66 | namespace { | ||
| 67 | |||
| 68 | /** | ||
| 69 | * Used for cas insensitive HasSuffix | ||
| 70 | */ | ||
| 71 | struct IgnoreCaseComperator { | ||
| 72 | 15709 | IgnoreCaseComperator() { } | |
| 73 | 581 | bool operator()(const std::string::value_type a, | |
| 74 | const std::string::value_type b) const { | ||
| 75 | 581 | return std::tolower(a) == std::tolower(b); | |
| 76 | } | ||
| 77 | }; | ||
| 78 | |||
| 79 | } // anonymous namespace | ||
| 80 | |||
| 81 |
3/4✓ Branch 1 taken 168 times.
✓ Branch 2 taken 3018 times.
✓ Branch 4 taken 3186 times.
✗ Branch 5 not taken.
|
3186 | string StringifyBool(const bool value) { return value ? "yes" : "no"; } |
| 82 | |||
| 83 | 25234029 | string StringifyInt(const int64_t value) { | |
| 84 | char buffer[48]; | ||
| 85 | 25234029 | snprintf(buffer, sizeof(buffer), "%" PRId64, value); | |
| 86 |
1/2✓ Branch 2 taken 25203453 times.
✗ Branch 3 not taken.
|
25234029 | return string(buffer); |
| 87 | } | ||
| 88 | |||
| 89 | 5500051 | std::string StringifyUint(const uint64_t value) { | |
| 90 | char buffer[48]; | ||
| 91 | 5500051 | snprintf(buffer, sizeof(buffer), "%" PRIu64, value); | |
| 92 |
1/2✓ Branch 2 taken 5500051 times.
✗ Branch 3 not taken.
|
5500051 | return string(buffer); |
| 93 | } | ||
| 94 | |||
| 95 | 168 | string StringifyByteAsHex(const unsigned char value) { | |
| 96 | char buffer[3]; | ||
| 97 | 168 | snprintf(buffer, sizeof(buffer), "%02x", value); | |
| 98 |
1/2✓ Branch 2 taken 168 times.
✗ Branch 3 not taken.
|
168 | return string(buffer); |
| 99 | } | ||
| 100 | |||
| 101 | 266 | string StringifyDouble(const double value) { | |
| 102 | char buffer[64]; | ||
| 103 | 266 | snprintf(buffer, sizeof(buffer), "%.03f", value); | |
| 104 |
1/2✓ Branch 2 taken 266 times.
✗ Branch 3 not taken.
|
266 | return string(buffer); |
| 105 | } | ||
| 106 | |||
| 107 | /** | ||
| 108 | * Converts seconds since UTC 0 into something readable | ||
| 109 | */ | ||
| 110 | 1561 | string StringifyTime(const time_t seconds, const bool utc) { | |
| 111 | struct tm timestamp; | ||
| 112 |
2/2✓ Branch 0 taken 837 times.
✓ Branch 1 taken 724 times.
|
1561 | if (utc) { |
| 113 | 837 | localtime_r(&seconds, ×tamp); | |
| 114 | } else { | ||
| 115 | 724 | gmtime_r(&seconds, ×tamp); | |
| 116 | } | ||
| 117 | |||
| 118 | 1561 | const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
| 119 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; | ||
| 120 | char buffer[21]; | ||
| 121 | 1561 | snprintf(buffer, sizeof(buffer), "%d %s %d %02d:%02d:%02d", timestamp.tm_mday, | |
| 122 | 1561 | months[timestamp.tm_mon], timestamp.tm_year + 1900, | |
| 123 | timestamp.tm_hour, timestamp.tm_min, timestamp.tm_sec); | ||
| 124 | |||
| 125 |
1/2✓ Branch 2 taken 1561 times.
✗ Branch 3 not taken.
|
1561 | return string(buffer); |
| 126 | } | ||
| 127 | |||
| 128 | /** | ||
| 129 | * Converts seconds since UTC 0 into something like 12 Sep 14:59:37 CDT | ||
| 130 | */ | ||
| 131 | 609 | string StringifyLocalTime(const time_t seconds) { | |
| 132 | struct tm timestamp; | ||
| 133 | 609 | localtime_r(&seconds, ×tamp); | |
| 134 | |||
| 135 | 609 | const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
| 136 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; | ||
| 137 | char buffer[26]; | ||
| 138 | (void)/* cast to void ignores return and placates clang-tidy */ | ||
| 139 | 609 | snprintf(buffer, sizeof(buffer), "%d %s %d %02d:%02d:%02d %s", | |
| 140 | 609 | timestamp.tm_mday, months[timestamp.tm_mon], | |
| 141 | 609 | timestamp.tm_year + 1900, timestamp.tm_hour, timestamp.tm_min, | |
| 142 | timestamp.tm_sec, timestamp.tm_zone); | ||
| 143 | |||
| 144 |
1/2✓ Branch 2 taken 609 times.
✗ Branch 3 not taken.
|
609 | return string(buffer); |
| 145 | } | ||
| 146 | |||
| 147 | |||
| 148 | /** | ||
| 149 | * Current time in format Wed, 01 Mar 2006 12:00:00 GMT | ||
| 150 | */ | ||
| 151 | 30619 | std::string RfcTimestamp() { | |
| 152 | 30619 | const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", | |
| 153 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; | ||
| 154 | 30619 | const char *day_of_week[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; | |
| 155 | |||
| 156 | struct tm timestamp; | ||
| 157 | 30619 | const time_t now = time(NULL); | |
| 158 | 30619 | gmtime_r(&now, ×tamp); | |
| 159 | |||
| 160 | char buffer[30]; | ||
| 161 | 30619 | snprintf(buffer, sizeof(buffer), "%s, %02d %s %d %02d:%02d:%02d %s", | |
| 162 | 30619 | day_of_week[timestamp.tm_wday], timestamp.tm_mday, | |
| 163 | 30619 | months[timestamp.tm_mon], timestamp.tm_year + 1900, | |
| 164 | timestamp.tm_hour, timestamp.tm_min, timestamp.tm_sec, | ||
| 165 | timestamp.tm_zone); | ||
| 166 |
1/2✓ Branch 2 taken 30619 times.
✗ Branch 3 not taken.
|
30619 | return string(buffer); |
| 167 | } | ||
| 168 | |||
| 169 | |||
| 170 | /** | ||
| 171 | * Current time in format YYYYMMDDTHHMMSSZ. Used in AWS4 requests. | ||
| 172 | */ | ||
| 173 | 45 | std::string IsoTimestamp() { | |
| 174 | struct tm timestamp; | ||
| 175 | 45 | const time_t now = time(NULL); | |
| 176 | 45 | gmtime_r(&now, ×tamp); | |
| 177 | |||
| 178 | char buffer[17]; | ||
| 179 | 45 | snprintf(buffer, sizeof(buffer), "%04d%02d%02dT%02d%02d%02dZ", | |
| 180 | 45 | timestamp.tm_year + 1900, timestamp.tm_mon + 1, timestamp.tm_mday, | |
| 181 | timestamp.tm_hour, timestamp.tm_min, timestamp.tm_sec); | ||
| 182 |
1/2✓ Branch 2 taken 45 times.
✗ Branch 3 not taken.
|
45 | return string(buffer); |
| 183 | } | ||
| 184 | |||
| 185 | |||
| 186 | /** | ||
| 187 | * UTC time in format YYYYMMDDHHMMSS. Used in cvmfs whitelists. | ||
| 188 | */ | ||
| 189 | 95 | std::string WhitelistTimestamp(time_t when) { | |
| 190 | struct tm timestamp; | ||
| 191 | 95 | gmtime_r(&when, ×tamp); | |
| 192 | |||
| 193 | char buffer[15]; | ||
| 194 | 95 | snprintf(buffer, sizeof(buffer), "%04d%02d%02d%02d%02d%02d", | |
| 195 | 95 | timestamp.tm_year + 1900, timestamp.tm_mon + 1, timestamp.tm_mday, | |
| 196 | timestamp.tm_hour, timestamp.tm_min, timestamp.tm_sec); | ||
| 197 |
1/2✓ Branch 2 taken 95 times.
✗ Branch 3 not taken.
|
95 | return string(buffer); |
| 198 | } | ||
| 199 | |||
| 200 | |||
| 201 | 4844276 | string StringifyTimeval(const timeval value) { | |
| 202 | char buffer[64]; | ||
| 203 | 4844276 | int64_t msec = value.tv_sec * 1000; | |
| 204 | 4844276 | msec += value.tv_usec / 1000; | |
| 205 | 4844276 | snprintf(buffer, sizeof(buffer), "%" PRId64 ".%03d", msec, | |
| 206 | 4844276 | static_cast<int>(value.tv_usec % 1000)); | |
| 207 |
1/2✓ Branch 2 taken 4844276 times.
✗ Branch 3 not taken.
|
4844276 | return string(buffer); |
| 208 | } | ||
| 209 | |||
| 210 | /** | ||
| 211 | * Parses a timestamp of the form YYYY-MM-DDTHH:MM:SSZ | ||
| 212 | * Return 0 on error | ||
| 213 | */ | ||
| 214 | 1124 | time_t IsoTimestamp2UtcTime(const std::string &iso8601) { | |
| 215 | 1124 | time_t utc_time = 0; | |
| 216 | 1124 | const unsigned length = iso8601.length(); | |
| 217 | |||
| 218 |
2/2✓ Branch 0 taken 82 times.
✓ Branch 1 taken 1042 times.
|
1124 | if (length != 20) |
| 219 | 82 | return utc_time; | |
| 220 |
2/4✓ Branch 2 taken 1042 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1042 times.
✗ Branch 6 not taken.
|
2084 | if ((iso8601[4] != '-') || (iso8601[7] != '-') || (iso8601[10] != 'T') |
| 221 |
5/10✓ Branch 0 taken 1042 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1042 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 1042 times.
✗ Branch 7 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 1042 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 1042 times.
|
2084 | || (iso8601[13] != ':') || (iso8601[16] != ':') || (iso8601[19] != 'Z')) { |
| 222 | ✗ | return utc_time; | |
| 223 | } | ||
| 224 | |||
| 225 | struct tm tm_wl; | ||
| 226 | 1042 | memset(&tm_wl, 0, sizeof(struct tm)); | |
| 227 |
2/4✓ Branch 1 taken 1042 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1042 times.
✗ Branch 5 not taken.
|
1042 | tm_wl.tm_year = static_cast<int>(String2Int64(iso8601.substr(0, 4))) - 1900; |
| 228 |
2/4✓ Branch 1 taken 1042 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1042 times.
✗ Branch 5 not taken.
|
1042 | tm_wl.tm_mon = static_cast<int>(String2Int64(iso8601.substr(5, 2))) - 1; |
| 229 |
2/4✓ Branch 1 taken 1042 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1042 times.
✗ Branch 5 not taken.
|
1042 | tm_wl.tm_mday = static_cast<int>(String2Int64(iso8601.substr(8, 2))); |
| 230 |
2/4✓ Branch 1 taken 1042 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1042 times.
✗ Branch 5 not taken.
|
1042 | tm_wl.tm_hour = static_cast<int>(String2Int64(iso8601.substr(11, 2))); |
| 231 |
2/4✓ Branch 1 taken 1042 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1042 times.
✗ Branch 5 not taken.
|
1042 | tm_wl.tm_min = static_cast<int>(String2Int64(iso8601.substr(14, 2))); |
| 232 |
2/4✓ Branch 1 taken 1042 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1042 times.
✗ Branch 5 not taken.
|
1042 | tm_wl.tm_sec = static_cast<int>(String2Int64(iso8601.substr(17, 2))); |
| 233 | 1042 | utc_time = timegm(&tm_wl); | |
| 234 |
2/2✓ Branch 0 taken 41 times.
✓ Branch 1 taken 1001 times.
|
1042 | if (utc_time < 0) |
| 235 | 41 | return 0; | |
| 236 | |||
| 237 | 1001 | return utc_time; | |
| 238 | } | ||
| 239 | |||
| 240 | 23898 | int64_t String2Int64(const string &value) { | |
| 241 | int64_t result; | ||
| 242 | 23898 | sscanf(value.c_str(), "%" PRId64, &result); | |
| 243 | 23898 | return result; | |
| 244 | } | ||
| 245 | |||
| 246 | 193255 | uint64_t String2Uint64(const string &value) { | |
| 247 | uint64_t result; | ||
| 248 |
2/2✓ Branch 1 taken 193179 times.
✓ Branch 2 taken 76 times.
|
193255 | if (sscanf(value.c_str(), "%" PRIu64, &result) == 1) { |
| 249 | 193179 | return result; | |
| 250 | } | ||
| 251 | 76 | return 0; | |
| 252 | } | ||
| 253 | |||
| 254 | /** | ||
| 255 | * Parse a string into a a uint64_t. | ||
| 256 | * | ||
| 257 | * Unlike String2Uint64, this: | ||
| 258 | * - Checks to make sure the full string is parsed | ||
| 259 | * - Can indicate an error occurred. | ||
| 260 | * | ||
| 261 | * If an error occurs, this returns false and sets errno appropriately. | ||
| 262 | */ | ||
| 263 | 4514 | bool String2Uint64Parse(const std::string &value, uint64_t *result) { | |
| 264 | 4514 | char *endptr = NULL; | |
| 265 | 4514 | errno = 0; | |
| 266 | 4514 | long long myval = strtoll(value.c_str(), &endptr, 10); // NOLINT | |
| 267 |
2/2✓ Branch 3 taken 3715 times.
✓ Branch 4 taken 692 times.
|
8921 | if ((value.size() == 0) || (endptr != (value.c_str() + value.size())) |
| 268 |
6/6✓ Branch 0 taken 4407 times.
✓ Branch 1 taken 107 times.
✓ Branch 2 taken 35 times.
✓ Branch 3 taken 3680 times.
✓ Branch 4 taken 834 times.
✓ Branch 5 taken 3680 times.
|
8921 | || (myval < 0)) { |
| 269 | 834 | errno = EINVAL; | |
| 270 | 834 | return false; | |
| 271 | } | ||
| 272 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3680 times.
|
3680 | if (errno) { |
| 273 | ✗ | return false; | |
| 274 | } | ||
| 275 |
2/2✓ Branch 0 taken 3645 times.
✓ Branch 1 taken 35 times.
|
3680 | if (result) { |
| 276 | 3645 | *result = myval; | |
| 277 | } | ||
| 278 | 3680 | return true; | |
| 279 | } | ||
| 280 | |||
| 281 | 132 | void String2Uint64Pair(const string &value, uint64_t *a, uint64_t *b) { | |
| 282 | 132 | sscanf(value.c_str(), "%" PRIu64 " %" PRIu64, a, b); | |
| 283 | 132 | } | |
| 284 | |||
| 285 | 210568 | bool HasPrefix(const string &str, const string &prefix, | |
| 286 | const bool ignore_case) { | ||
| 287 |
2/2✓ Branch 2 taken 38274 times.
✓ Branch 3 taken 172294 times.
|
210568 | if (prefix.length() > str.length()) |
| 288 | 38274 | return false; | |
| 289 | |||
| 290 |
2/2✓ Branch 1 taken 544644 times.
✓ Branch 2 taken 45983 times.
|
590627 | for (unsigned i = 0, l = prefix.length(); i < l; ++i) { |
| 291 |
2/2✓ Branch 0 taken 125911 times.
✓ Branch 1 taken 418733 times.
|
544644 | if (ignore_case) { |
| 292 |
2/2✓ Branch 2 taken 68536 times.
✓ Branch 3 taken 57375 times.
|
125911 | if (toupper(str[i]) != toupper(prefix[i])) |
| 293 | 68536 | return false; | |
| 294 | } else { | ||
| 295 |
2/2✓ Branch 2 taken 57775 times.
✓ Branch 3 taken 360958 times.
|
418733 | if (str[i] != prefix[i]) |
| 296 | 57775 | return false; | |
| 297 | } | ||
| 298 | } | ||
| 299 | 45983 | return true; | |
| 300 | } | ||
| 301 | |||
| 302 | 15966 | bool HasSuffix(const std::string &str, const std::string &suffix, | |
| 303 | const bool ignore_case) { | ||
| 304 |
2/2✓ Branch 2 taken 257 times.
✓ Branch 3 taken 15709 times.
|
15966 | if (suffix.size() > str.size()) |
| 305 | 257 | return false; | |
| 306 | 15709 | const IgnoreCaseComperator icmp; | |
| 307 | return (ignore_case) | ||
| 308 |
3/4✓ Branch 0 taken 307 times.
✓ Branch 1 taken 15402 times.
✓ Branch 6 taken 307 times.
✗ Branch 7 not taken.
|
15709 | ? std::equal(suffix.rbegin(), suffix.rend(), str.rbegin(), icmp) |
| 309 |
1/2✓ Branch 4 taken 15402 times.
✗ Branch 5 not taken.
|
15709 | : std::equal(suffix.rbegin(), suffix.rend(), str.rbegin()); |
| 310 | } | ||
| 311 | |||
| 312 | 41859 | vector<string> SplitString(const string &str, char delim) { | |
| 313 | 41859 | return SplitStringBounded(0, str, delim); | |
| 314 | } | ||
| 315 | |||
| 316 | 42099 | vector<string> SplitStringBounded(unsigned max_chunks, const string &str, | |
| 317 | char delim) { | ||
| 318 | 42099 | vector<string> result; | |
| 319 | |||
| 320 | // edge case... one chunk is always the whole string | ||
| 321 |
2/2✓ Branch 0 taken 40 times.
✓ Branch 1 taken 42059 times.
|
42099 | if (1 == max_chunks) { |
| 322 |
1/2✓ Branch 1 taken 40 times.
✗ Branch 2 not taken.
|
40 | result.push_back(str); |
| 323 | 40 | return result; | |
| 324 | } | ||
| 325 | |||
| 326 | // split the string | ||
| 327 | 42059 | const unsigned size = str.size(); | |
| 328 | 42059 | unsigned marker = 0; | |
| 329 | 42059 | unsigned chunks = 1; | |
| 330 | unsigned i; | ||
| 331 |
2/2✓ Branch 0 taken 1047838 times.
✓ Branch 1 taken 41979 times.
|
1089817 | for (i = 0; i < size; ++i) { |
| 332 |
2/2✓ Branch 1 taken 70093 times.
✓ Branch 2 taken 977745 times.
|
1047838 | if (str[i] == delim) { |
| 333 |
2/4✓ Branch 1 taken 70093 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 70093 times.
✗ Branch 5 not taken.
|
70093 | result.push_back(str.substr(marker, i - marker)); |
| 334 | 70093 | marker = i + 1; | |
| 335 | |||
| 336 | // we got what we want... good bye | ||
| 337 |
2/2✓ Branch 0 taken 80 times.
✓ Branch 1 taken 70013 times.
|
70093 | if (++chunks == max_chunks) |
| 338 | 80 | break; | |
| 339 | } | ||
| 340 | } | ||
| 341 | |||
| 342 | // push the remainings of the string and return | ||
| 343 |
2/4✓ Branch 1 taken 42059 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 42059 times.
✗ Branch 5 not taken.
|
42059 | result.push_back(str.substr(marker)); |
| 344 | 42059 | return result; | |
| 345 | } | ||
| 346 | |||
| 347 | 123 | vector<string> SplitStringMultiChar(const string &str, const string &delim) { | |
| 348 | 123 | size_t pos_start = 0, pos_end = 0, delim_len = delim.length(); | |
| 349 | 123 | std::string substring; | |
| 350 | 123 | std::vector<std::string> result; | |
| 351 | |||
| 352 |
2/2✓ Branch 1 taken 451 times.
✓ Branch 2 taken 123 times.
|
574 | while ((pos_end = str.find(delim, pos_start)) != string::npos) { |
| 353 |
1/2✓ Branch 1 taken 451 times.
✗ Branch 2 not taken.
|
451 | substring = str.substr(pos_start, pos_end - pos_start); |
| 354 | 451 | pos_start = pos_end + delim_len; | |
| 355 |
1/2✓ Branch 1 taken 451 times.
✗ Branch 2 not taken.
|
451 | result.push_back(substring); |
| 356 | } | ||
| 357 | |||
| 358 |
2/4✓ Branch 1 taken 123 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 123 times.
✗ Branch 5 not taken.
|
123 | result.push_back(str.substr(pos_start)); |
| 359 | 246 | return result; | |
| 360 | 123 | } | |
| 361 | |||
| 362 | 6719 | string JoinStrings(const vector<string> &strings, const string &joint) { | |
| 363 |
1/2✓ Branch 2 taken 6719 times.
✗ Branch 3 not taken.
|
6719 | string result = ""; |
| 364 | 6719 | const unsigned size = strings.size(); | |
| 365 | |||
| 366 |
2/2✓ Branch 0 taken 4955 times.
✓ Branch 1 taken 1764 times.
|
6719 | if (size > 0) { |
| 367 |
1/2✓ Branch 2 taken 4955 times.
✗ Branch 3 not taken.
|
4955 | result = strings[0]; |
| 368 |
2/2✓ Branch 0 taken 1289 times.
✓ Branch 1 taken 4955 times.
|
6244 | for (unsigned i = 1; i < size; ++i) |
| 369 |
2/4✓ Branch 2 taken 1289 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1289 times.
✗ Branch 6 not taken.
|
1289 | result += joint + strings[i]; |
| 370 | } | ||
| 371 | |||
| 372 | 6719 | return result; | |
| 373 | } | ||
| 374 | |||
| 375 | 2210 | void ParseKeyvalMem(const unsigned char *buffer, const unsigned buffer_size, | |
| 376 | map<char, string> *content) { | ||
| 377 | 2210 | string line; | |
| 378 | 2210 | unsigned pos = 0; | |
| 379 |
2/2✓ Branch 0 taken 215392 times.
✓ Branch 1 taken 38 times.
|
215430 | while (pos < buffer_size) { |
| 380 |
2/2✓ Branch 0 taken 15767 times.
✓ Branch 1 taken 199625 times.
|
215392 | if (static_cast<char>(buffer[pos]) == '\n') { |
| 381 |
2/2✓ Branch 1 taken 2172 times.
✓ Branch 2 taken 13595 times.
|
15767 | if (line == "--") |
| 382 | 2172 | return; | |
| 383 | |||
| 384 |
1/2✓ Branch 1 taken 13595 times.
✗ Branch 2 not taken.
|
13595 | if (line != "") { |
| 385 |
3/11✗ Branch 1 not taken.
✓ Branch 2 taken 13595 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 13595 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 13595 times.
✗ Branch 12 not taken.
✗ Branch 13 not taken.
|
13595 | const string tail = (line.length() == 1) ? "" : line.substr(1); |
| 386 | // Special handling of 'Z' key because it can exist multiple times | ||
| 387 |
3/4✓ Branch 1 taken 13595 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 13433 times.
✓ Branch 4 taken 162 times.
|
13595 | if (line[0] != 'Z') { |
| 388 |
3/6✓ Branch 1 taken 13433 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 13433 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 13433 times.
✗ Branch 8 not taken.
|
13433 | (*content)[line[0]] = tail; |
| 389 | } else { | ||
| 390 |
4/7✓ Branch 2 taken 162 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 162 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 81 times.
✓ Branch 9 taken 81 times.
|
162 | if (content->find(line[0]) == content->end()) { |
| 391 |
3/6✓ Branch 1 taken 81 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 81 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 81 times.
✗ Branch 8 not taken.
|
81 | (*content)[line[0]] = tail; |
| 392 | } else { | ||
| 393 |
6/12✓ Branch 1 taken 81 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 81 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 81 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 81 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 81 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 81 times.
✗ Branch 17 not taken.
|
81 | (*content)[line[0]] = (*content)[line[0]] + "|" + tail; |
| 394 | } | ||
| 395 | } | ||
| 396 | 13595 | } | |
| 397 |
1/2✓ Branch 1 taken 13595 times.
✗ Branch 2 not taken.
|
13595 | line = ""; |
| 398 | } else { | ||
| 399 |
1/2✓ Branch 1 taken 199625 times.
✗ Branch 2 not taken.
|
199625 | line += static_cast<char>(buffer[pos]); |
| 400 | } | ||
| 401 | 213220 | pos++; | |
| 402 | } | ||
| 403 |
2/2✓ Branch 1 taken 38 times.
✓ Branch 2 taken 2172 times.
|
2210 | } |
| 404 | |||
| 405 | 318 | bool ParseKeyvalPath(const string &filename, map<char, string> *content) { | |
| 406 |
1/2✓ Branch 2 taken 318 times.
✗ Branch 3 not taken.
|
318 | const int fd = open(filename.c_str(), O_RDONLY); |
| 407 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 276 times.
|
318 | if (fd < 0) |
| 408 | 42 | return false; | |
| 409 | |||
| 410 | unsigned char buffer[4096]; | ||
| 411 |
1/2✓ Branch 1 taken 276 times.
✗ Branch 2 not taken.
|
276 | const ssize_t num_bytes = read(fd, buffer, sizeof(buffer)); |
| 412 |
1/2✓ Branch 1 taken 276 times.
✗ Branch 2 not taken.
|
276 | close(fd); |
| 413 | |||
| 414 |
3/4✓ Branch 0 taken 234 times.
✓ Branch 1 taken 42 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 234 times.
|
276 | if ((num_bytes <= 0) || (unsigned(num_bytes) >= sizeof(buffer))) |
| 415 | 42 | return false; | |
| 416 | |||
| 417 |
1/2✓ Branch 1 taken 234 times.
✗ Branch 2 not taken.
|
234 | ParseKeyvalMem(buffer, unsigned(num_bytes), content); |
| 418 | 234 | return true; | |
| 419 | } | ||
| 420 | |||
| 421 | 113614 | string GetLineMem(const char *text, const int text_size) { | |
| 422 | 113614 | int pos = 0; | |
| 423 |
4/4✓ Branch 0 taken 4758910 times.
✓ Branch 1 taken 303 times.
✓ Branch 2 taken 4645599 times.
✓ Branch 3 taken 113311 times.
|
4759213 | while ((pos < text_size) && (text[pos] != '\n')) |
| 424 | 4645599 | pos++; | |
| 425 |
1/2✓ Branch 2 taken 113614 times.
✗ Branch 3 not taken.
|
113614 | return string(text, pos); |
| 426 | } | ||
| 427 | |||
| 428 | 4871430 | bool GetLineFile(FILE *f, std::string *line) { | |
| 429 | int retval; | ||
| 430 | 4871430 | line->clear(); | |
| 431 | while (true) { | ||
| 432 | 346810056 | retval = fgetc(f); | |
| 433 |
2/6✗ Branch 1 not taken.
✓ Branch 2 taken 346810056 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 346810056 times.
|
346810056 | if (ferror(f) && (errno == EINTR)) { |
| 434 | ✗ | clearerr(f); | |
| 435 | ✗ | continue; | |
| 436 |
2/2✓ Branch 0 taken 5586 times.
✓ Branch 1 taken 346804470 times.
|
346810056 | } else if (retval == EOF) { |
| 437 | 5586 | break; | |
| 438 | } | ||
| 439 | 346804470 | const char c = static_cast<char>(retval); | |
| 440 |
2/2✓ Branch 0 taken 4865844 times.
✓ Branch 1 taken 341938626 times.
|
346804470 | if (c == '\n') |
| 441 | 4865844 | break; | |
| 442 | 341938626 | line->push_back(c); | |
| 443 | 341938626 | } | |
| 444 |
4/4✓ Branch 0 taken 5586 times.
✓ Branch 1 taken 4865844 times.
✓ Branch 3 taken 1133 times.
✓ Branch 4 taken 4453 times.
|
4871430 | return (retval != EOF) || !line->empty(); |
| 445 | } | ||
| 446 | |||
| 447 | 4430 | bool GetLineFd(const int fd, std::string *line) { | |
| 448 | ssize_t retval; | ||
| 449 | char c; | ||
| 450 | 4430 | line->clear(); | |
| 451 | while (true) { | ||
| 452 |
1/2✓ Branch 1 taken 199863 times.
✗ Branch 2 not taken.
|
199863 | retval = read(fd, &c, 1); |
| 453 |
2/2✓ Branch 0 taken 104 times.
✓ Branch 1 taken 199759 times.
|
199863 | if (retval == 0) { |
| 454 | 104 | break; | |
| 455 | } | ||
| 456 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 199759 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
199759 | if ((retval == -1) && (errno == EINTR)) { |
| 457 | ✗ | continue; | |
| 458 | } | ||
| 459 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 199759 times.
|
199759 | if (retval == -1) { |
| 460 | ✗ | break; | |
| 461 | } | ||
| 462 |
2/2✓ Branch 0 taken 4326 times.
✓ Branch 1 taken 195433 times.
|
199759 | if (c == '\n') |
| 463 | 4326 | break; | |
| 464 |
1/2✓ Branch 1 taken 195433 times.
✗ Branch 2 not taken.
|
195433 | line->push_back(c); |
| 465 | } | ||
| 466 |
4/4✓ Branch 0 taken 104 times.
✓ Branch 1 taken 4326 times.
✓ Branch 3 taken 75 times.
✓ Branch 4 taken 29 times.
|
4430 | return (retval == 1) || !line->empty(); |
| 467 | } | ||
| 468 | |||
| 469 | /** | ||
| 470 | * Removes leading and trailing whitespaces. | ||
| 471 | */ | ||
| 472 | 21889 | string Trim(const string &raw, bool trim_newline) { | |
| 473 |
2/2✓ Branch 1 taken 1777 times.
✓ Branch 2 taken 20112 times.
|
21889 | if (raw.empty()) |
| 474 |
1/2✓ Branch 2 taken 1777 times.
✗ Branch 3 not taken.
|
1777 | return ""; |
| 475 | |||
| 476 | 20112 | unsigned start_pos = 0; | |
| 477 | 22831 | for (; (start_pos < raw.length()) | |
| 478 |
7/8✓ Branch 0 taken 22279 times.
✓ Branch 1 taken 552 times.
✓ Branch 3 taken 20705 times.
✓ Branch 4 taken 1574 times.
✓ Branch 6 taken 20705 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2719 times.
✓ Branch 9 taken 20112 times.
|
43536 | && (raw[start_pos] == ' ' || raw[start_pos] == '\t' |
| 479 |
2/2✓ Branch 0 taken 4495 times.
✓ Branch 1 taken 16210 times.
|
20705 | || (trim_newline |
| 480 |
4/4✓ Branch 1 taken 3943 times.
✓ Branch 2 taken 552 times.
✓ Branch 4 taken 593 times.
✓ Branch 5 taken 3350 times.
|
4495 | && (raw[start_pos] == '\n' || raw[start_pos] == '\r'))); |
| 481 | ++start_pos) { | ||
| 482 | } | ||
| 483 | 20112 | unsigned end_pos = raw.length() - 1; // at least one character in raw | |
| 484 | 27910 | for (; | |
| 485 | (end_pos >= start_pos) | ||
| 486 |
7/8✓ Branch 0 taken 27358 times.
✓ Branch 1 taken 552 times.
✓ Branch 3 taken 26061 times.
✓ Branch 4 taken 1297 times.
✓ Branch 6 taken 26061 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 7798 times.
✓ Branch 9 taken 20112 times.
|
53971 | && (raw[end_pos] == ' ' || raw[end_pos] == '\t' |
| 487 |
6/6✓ Branch 0 taken 9851 times.
✓ Branch 1 taken 16210 times.
✓ Branch 3 taken 6181 times.
✓ Branch 4 taken 3670 times.
✓ Branch 6 taken 2831 times.
✓ Branch 7 taken 3350 times.
|
26061 | || (trim_newline && (raw[end_pos] == '\n' || raw[end_pos] == '\r'))); |
| 488 | --end_pos) { | ||
| 489 | } | ||
| 490 | |||
| 491 | 20112 | return raw.substr(start_pos, end_pos - start_pos + 1); | |
| 492 | } | ||
| 493 | |||
| 494 | 60 | std::string TrimString(const std::string &path, | |
| 495 | const std::string &toTrim, | ||
| 496 | const int trimMode) { | ||
| 497 | 60 | std::string trimmed = path; | |
| 498 |
1/2✓ Branch 1 taken 60 times.
✗ Branch 2 not taken.
|
60 | if (trimmed != toTrim) { |
| 499 |
3/4✓ Branch 1 taken 120 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 90 times.
✓ Branch 4 taken 30 times.
|
180 | while ((trimMode & kTrimLeading) && HasPrefix(trimmed, toTrim, true) |
| 500 |
5/6✓ Branch 0 taken 120 times.
✓ Branch 1 taken 30 times.
✓ Branch 4 taken 90 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 90 times.
✓ Branch 7 taken 60 times.
|
270 | && (trimmed.size() > toTrim.size())) { |
| 501 |
1/2✓ Branch 2 taken 90 times.
✗ Branch 3 not taken.
|
90 | trimmed = trimmed.substr(toTrim.size()); |
| 502 | } | ||
| 503 |
3/4✓ Branch 1 taken 90 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 60 times.
✓ Branch 4 taken 30 times.
|
150 | while ((trimMode & kTrimTrailing) && HasSuffix(trimmed, toTrim, true) |
| 504 |
5/6✓ Branch 0 taken 90 times.
✓ Branch 1 taken 30 times.
✓ Branch 4 taken 60 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 60 times.
✓ Branch 7 taken 60 times.
|
210 | && (trimmed.size() > toTrim.size())) { |
| 505 |
1/2✓ Branch 3 taken 60 times.
✗ Branch 4 not taken.
|
60 | trimmed = trimmed.substr(0, trimmed.size() - toTrim.size()); |
| 506 | } | ||
| 507 | } | ||
| 508 | 60 | return trimmed; | |
| 509 | } | ||
| 510 | |||
| 511 | /** | ||
| 512 | * Converts all characters to upper case | ||
| 513 | */ | ||
| 514 | 1971 | string ToUpper(const string &mixed_case) { | |
| 515 | 1971 | string result(mixed_case); | |
| 516 |
2/2✓ Branch 1 taken 5606 times.
✓ Branch 2 taken 1971 times.
|
7577 | for (unsigned i = 0, l = result.length(); i < l; ++i) { |
| 517 |
2/4✓ Branch 1 taken 5606 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 5606 times.
✗ Branch 5 not taken.
|
5606 | result[i] = static_cast<char>(toupper(result[i])); |
| 518 | } | ||
| 519 | 1971 | return result; | |
| 520 | } | ||
| 521 | |||
| 522 | 4383 | string ReplaceAll(const string &haystack, const string &needle, | |
| 523 | const string &replace_by) { | ||
| 524 | 4383 | string result(haystack); | |
| 525 | 4383 | size_t pos = 0; | |
| 526 | 4383 | const unsigned needle_size = needle.size(); | |
| 527 |
2/2✓ Branch 1 taken 45 times.
✓ Branch 2 taken 4338 times.
|
4383 | if (needle == "") |
| 528 | 45 | return result; | |
| 529 | |||
| 530 |
2/2✓ Branch 1 taken 2834 times.
✓ Branch 2 taken 4338 times.
|
7172 | while ((pos = result.find(needle, pos)) != string::npos) |
| 531 |
1/2✓ Branch 1 taken 2834 times.
✗ Branch 2 not taken.
|
2834 | result.replace(pos, needle_size, replace_by); |
| 532 | 4338 | return result; | |
| 533 | } | ||
| 534 | |||
| 535 | 429113 | static inline void Base64Block(const unsigned char input[3], const char *table, | |
| 536 | char output[4]) { | ||
| 537 | 429113 | output[0] = table[(input[0] & 0xFD) >> 2]; | |
| 538 | 429113 | output[1] = table[((input[0] & 0x03) << 4) | ((input[1] & 0xF0) >> 4)]; | |
| 539 | 429113 | output[2] = table[((input[1] & 0x0F) << 2) | ((input[2] & 0xD0) >> 6)]; | |
| 540 | 429113 | output[3] = table[input[2] & 0x3F]; | |
| 541 | 429113 | } | |
| 542 | |||
| 543 | 41507 | string Base64(const string &data) { | |
| 544 | 41507 | string result; | |
| 545 |
1/2✓ Branch 2 taken 41507 times.
✗ Branch 3 not taken.
|
41507 | result.reserve((data.length() + 3) * 4 / 3); |
| 546 | 41507 | unsigned pos = 0; | |
| 547 | const unsigned char *data_ptr = reinterpret_cast<const unsigned char *>( | ||
| 548 | 41507 | data.data()); | |
| 549 | 41507 | const unsigned length = data.length(); | |
| 550 |
2/2✓ Branch 0 taken 388071 times.
✓ Branch 1 taken 41507 times.
|
429578 | while (pos + 2 < length) { |
| 551 | char encoded_block[4]; | ||
| 552 | 388071 | Base64Block(data_ptr + pos, b64_table, encoded_block); | |
| 553 |
1/2✓ Branch 1 taken 388071 times.
✗ Branch 2 not taken.
|
388071 | result.append(encoded_block, 4); |
| 554 | 388071 | pos += 3; | |
| 555 | } | ||
| 556 |
2/2✓ Branch 0 taken 41042 times.
✓ Branch 1 taken 465 times.
|
41507 | if (length % 3 != 0) { |
| 557 | unsigned char input[3]; | ||
| 558 | 41042 | input[0] = data_ptr[pos]; | |
| 559 |
2/2✓ Branch 0 taken 27438 times.
✓ Branch 1 taken 13604 times.
|
41042 | input[1] = ((length % 3) == 2) ? data_ptr[pos + 1] : 0; |
| 560 | 41042 | input[2] = 0; | |
| 561 | char encoded_block[4]; | ||
| 562 | 41042 | Base64Block(input, b64_table, encoded_block); | |
| 563 |
1/2✓ Branch 1 taken 41042 times.
✗ Branch 2 not taken.
|
41042 | result.append(encoded_block, 2); |
| 564 |
3/4✓ Branch 0 taken 27438 times.
✓ Branch 1 taken 13604 times.
✓ Branch 3 taken 41042 times.
✗ Branch 4 not taken.
|
41042 | result.push_back(((length % 3) == 2) ? encoded_block[2] : '='); |
| 565 |
1/2✓ Branch 1 taken 41042 times.
✗ Branch 2 not taken.
|
41042 | result.push_back('='); |
| 566 | } | ||
| 567 | |||
| 568 | 41507 | return result; | |
| 569 | } | ||
| 570 | |||
| 571 | /** | ||
| 572 | * Safe encoding for URIs and path names: replace + by - and / by _ | ||
| 573 | */ | ||
| 574 | 85 | string Base64Url(const string &data) { | |
| 575 | 85 | string base64 = Base64(data); | |
| 576 |
2/2✓ Branch 1 taken 7140 times.
✓ Branch 2 taken 85 times.
|
7225 | for (unsigned i = 0, l = base64.length(); i < l; ++i) { |
| 577 |
3/4✓ Branch 1 taken 7140 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 102 times.
✓ Branch 4 taken 7038 times.
|
7140 | if (base64[i] == '+') { |
| 578 |
1/2✓ Branch 1 taken 102 times.
✗ Branch 2 not taken.
|
102 | base64[i] = '-'; |
| 579 |
3/4✓ Branch 1 taken 7038 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 68 times.
✓ Branch 4 taken 6970 times.
|
7038 | } else if (base64[i] == '/') { |
| 580 |
1/2✓ Branch 1 taken 68 times.
✗ Branch 2 not taken.
|
68 | base64[i] = '_'; |
| 581 | } | ||
| 582 | } | ||
| 583 | 85 | return base64; | |
| 584 | } | ||
| 585 | |||
| 586 | 1150299 | static bool Debase64Block(const unsigned char input[4], | |
| 587 | unsigned char output[3]) { | ||
| 588 | int32_t dec[4]; | ||
| 589 |
2/2✓ Branch 0 taken 4601145 times.
✓ Branch 1 taken 1150282 times.
|
5751427 | for (int i = 0; i < 4; ++i) { |
| 590 | 4601145 | dec[i] = db64_table[input[i]]; | |
| 591 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 4601128 times.
|
4601145 | if (dec[i] < 0) |
| 592 | 17 | return false; | |
| 593 | } | ||
| 594 | |||
| 595 | 1150282 | output[0] = (dec[0] << 2) | (dec[1] >> 4); | |
| 596 | 1150282 | output[1] = ((dec[1] & 0x0F) << 4) | (dec[2] >> 2); | |
| 597 | 1150282 | output[2] = ((dec[2] & 0x03) << 6) | dec[3]; | |
| 598 | 1150282 | return true; | |
| 599 | } | ||
| 600 | |||
| 601 | /** | ||
| 602 | * Can decode both base64 and base64url | ||
| 603 | */ | ||
| 604 | 5425 | bool Debase64(const string &data, string *decoded) { | |
| 605 | 5425 | decoded->clear(); | |
| 606 | 5425 | decoded->reserve((data.length() + 4) * 3 / 4); | |
| 607 | 5425 | unsigned pos = 0; | |
| 608 | const unsigned char *data_ptr = reinterpret_cast<const unsigned char *>( | ||
| 609 | 5425 | data.data()); | |
| 610 | 5425 | const unsigned length = data.length(); | |
| 611 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 5408 times.
|
5425 | if (length == 0) |
| 612 | 17 | return true; | |
| 613 |
2/2✓ Branch 0 taken 34 times.
✓ Branch 1 taken 5374 times.
|
5408 | if ((length % 4) != 0) |
| 614 | 34 | return false; | |
| 615 | |||
| 616 |
2/2✓ Branch 0 taken 1150299 times.
✓ Branch 1 taken 5357 times.
|
1155656 | while (pos < length) { |
| 617 | unsigned char decoded_block[3]; | ||
| 618 | 1150299 | const bool retval = Debase64Block(data_ptr + pos, decoded_block); | |
| 619 |
2/2✓ Branch 0 taken 17 times.
✓ Branch 1 taken 1150282 times.
|
1150299 | if (!retval) |
| 620 | 17 | return false; | |
| 621 |
1/2✓ Branch 1 taken 1150282 times.
✗ Branch 2 not taken.
|
1150282 | decoded->append(reinterpret_cast<char *>(decoded_block), 3); |
| 622 | 1150282 | pos += 4; | |
| 623 | } | ||
| 624 | |||
| 625 |
2/2✓ Branch 0 taken 10714 times.
✓ Branch 1 taken 5357 times.
|
16071 | for (int i = 0; i < 2; ++i) { |
| 626 | 10714 | pos--; | |
| 627 |
2/2✓ Branch 1 taken 1788 times.
✓ Branch 2 taken 8926 times.
|
10714 | if (data[pos] == '=') |
| 628 | 1788 | decoded->erase(decoded->length() - 1); | |
| 629 | } | ||
| 630 | 5357 | return true; | |
| 631 | } | ||
| 632 | |||
| 633 | /** | ||
| 634 | * Assumes that source is terminated by a newline | ||
| 635 | */ | ||
| 636 | 266 | string Tail(const string &source, unsigned num_lines) { | |
| 637 |
6/6✓ Branch 1 taken 190 times.
✓ Branch 2 taken 76 times.
✓ Branch 3 taken 38 times.
✓ Branch 4 taken 152 times.
✓ Branch 5 taken 114 times.
✓ Branch 6 taken 152 times.
|
266 | if (source.empty() || (num_lines == 0)) |
| 638 |
1/2✓ Branch 2 taken 114 times.
✗ Branch 3 not taken.
|
114 | return ""; |
| 639 | |||
| 640 | 152 | const int l = static_cast<int>(source.length()); | |
| 641 | 152 | int i = l - 1; | |
| 642 |
2/2✓ Branch 0 taken 608 times.
✓ Branch 1 taken 114 times.
|
722 | for (; i >= 0; --i) { |
| 643 | 608 | const char c = source.data()[i]; | |
| 644 |
2/2✓ Branch 0 taken 190 times.
✓ Branch 1 taken 418 times.
|
608 | if (c == '\n') { |
| 645 |
2/2✓ Branch 0 taken 38 times.
✓ Branch 1 taken 152 times.
|
190 | if (num_lines == 0) { |
| 646 | 38 | return source.substr(i + 1); | |
| 647 | } | ||
| 648 | 152 | num_lines--; | |
| 649 | } | ||
| 650 | } | ||
| 651 | 114 | return source; | |
| 652 | } | ||
| 653 | |||
| 654 | /** | ||
| 655 | * Get UTC Time. | ||
| 656 | * | ||
| 657 | * @param format format if timestamp (YYYY-MM-DD HH:MM:SS by default) | ||
| 658 | * @return a timestamp string on success, empty string on failure | ||
| 659 | */ | ||
| 660 | ✗ | std::string GetGMTimestamp(const std::string &format) { | |
| 661 | struct tm time_ptr; | ||
| 662 | char date_and_time[100]; | ||
| 663 | ✗ | const time_t t = time(NULL); | |
| 664 | ✗ | gmtime_r(&t, &time_ptr); // take UTC | |
| 665 | // return empty string if formatting fails | ||
| 666 | ✗ | if (!strftime(date_and_time, 100, format.c_str(), &time_ptr)) { | |
| 667 | ✗ | return ""; | |
| 668 | } | ||
| 669 | ✗ | std::string timestamp(date_and_time); | |
| 670 | ✗ | return timestamp; | |
| 671 | } | ||
| 672 | |||
| 673 | #ifdef CVMFS_NAMESPACE_GUARD | ||
| 674 | } // namespace CVMFS_NAMESPACE_GUARD | ||
| 675 | #endif | ||
| 676 |