CernVM-FS  2.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
string.cc
Go to the documentation of this file.
1 
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 
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 
68  bool operator()(const std::string::value_type a,
69  const std::string::value_type b) const {
70  return std::tolower(a) == std::tolower(b);
71  }
72 };
73 
74 } // anonymous namespace
75 
76 string StringifyBool(const bool value) { return value ? "yes" : "no"; }
77 
78 string StringifyInt(const int64_t value) {
79  char buffer[48];
80  snprintf(buffer, sizeof(buffer), "%" PRId64, value);
81  return string(buffer);
82 }
83 
84 std::string StringifyUint(const uint64_t value) {
85  char buffer[48];
86  snprintf(buffer, sizeof(buffer), "%" PRIu64, value);
87  return string(buffer);
88 }
89 
90 string StringifyByteAsHex(const unsigned char value) {
91  char buffer[3];
92  snprintf(buffer, sizeof(buffer), "%02x", value);
93  return string(buffer);
94 }
95 
96 string StringifyDouble(const double value) {
97  char buffer[64];
98  snprintf(buffer, sizeof(buffer), "%.03f", value);
99  return string(buffer);
100 }
101 
105 string StringifyTime(const time_t seconds, const bool utc) {
106  struct tm timestamp;
107  if (utc) {
108  localtime_r(&seconds, &timestamp);
109  } else {
110  gmtime_r(&seconds, &timestamp);
111  }
112 
113  const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
114  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
115  char buffer[21];
116  snprintf(buffer, sizeof(buffer), "%d %s %d %02d:%02d:%02d", timestamp.tm_mday,
117  months[timestamp.tm_mon], timestamp.tm_year + 1900,
118  timestamp.tm_hour, timestamp.tm_min, timestamp.tm_sec);
119 
120  return string(buffer);
121 }
122 
123 
127 std::string RfcTimestamp() {
128  const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
129  "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
130  const char *day_of_week[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
131 
132  struct tm timestamp;
133  time_t now = time(NULL);
134  gmtime_r(&now, &timestamp);
135 
136  char buffer[30];
137  snprintf(buffer, sizeof(buffer), "%s, %02d %s %d %02d:%02d:%02d %s",
138  day_of_week[timestamp.tm_wday], timestamp.tm_mday,
139  months[timestamp.tm_mon], timestamp.tm_year + 1900,
140  timestamp.tm_hour, timestamp.tm_min, timestamp.tm_sec,
141  timestamp.tm_zone);
142  return string(buffer);
143 }
144 
145 
149 std::string IsoTimestamp() {
150  struct tm timestamp;
151  time_t now = time(NULL);
152  gmtime_r(&now, &timestamp);
153 
154  char buffer[17];
155  snprintf(buffer, sizeof(buffer), "%04d%02d%02dT%02d%02d%02dZ",
156  timestamp.tm_year + 1900,
157  timestamp.tm_mon + 1,
158  timestamp.tm_mday,
159  timestamp.tm_hour,
160  timestamp.tm_min,
161  timestamp.tm_sec);
162  return string(buffer);
163 }
164 
165 
169 std::string WhitelistTimestamp(time_t when) {
170  struct tm timestamp;
171  gmtime_r(&when, &timestamp);
172 
173  char buffer[15];
174  snprintf(buffer, sizeof(buffer), "%04d%02d%02d%02d%02d%02d",
175  timestamp.tm_year + 1900,
176  timestamp.tm_mon + 1,
177  timestamp.tm_mday,
178  timestamp.tm_hour,
179  timestamp.tm_min,
180  timestamp.tm_sec);
181  return string(buffer);
182 }
183 
184 
185 string StringifyTimeval(const timeval value) {
186  char buffer[64];
187  int64_t msec = value.tv_sec * 1000;
188  msec += value.tv_usec / 1000;
189  snprintf(buffer, sizeof(buffer), "%" PRId64 ".%03d", msec,
190  static_cast<int>(value.tv_usec % 1000));
191  return string(buffer);
192 }
193 
198 time_t IsoTimestamp2UtcTime(const std::string &iso8601) {
199  time_t utc_time = 0;
200  unsigned length = iso8601.length();
201 
202  if (length != 20) return utc_time;
203  if ((iso8601[4] != '-') || (iso8601[7] != '-') || (iso8601[10] != 'T') ||
204  (iso8601[13] != ':') || (iso8601[16] != ':') || (iso8601[19] != 'Z')) {
205  return utc_time;
206  }
207 
208  struct tm tm_wl;
209  memset(&tm_wl, 0, sizeof(struct tm));
210  tm_wl.tm_year = static_cast<int>(String2Int64(iso8601.substr(0, 4))) - 1900;
211  tm_wl.tm_mon = static_cast<int>(String2Int64(iso8601.substr(5, 2))) - 1;
212  tm_wl.tm_mday = static_cast<int>(String2Int64(iso8601.substr(8, 2)));
213  tm_wl.tm_hour = static_cast<int>(String2Int64(iso8601.substr(11, 2)));
214  tm_wl.tm_min = static_cast<int>(String2Int64(iso8601.substr(14, 2)));
215  tm_wl.tm_sec = static_cast<int>(String2Int64(iso8601.substr(17, 2)));
216  utc_time = timegm(&tm_wl);
217  if (utc_time < 0) return 0;
218 
219  return utc_time;
220 }
221 
222 int64_t String2Int64(const string &value) {
223  int64_t result;
224  sscanf(value.c_str(), "%" PRId64, &result);
225  return result;
226 }
227 
228 uint64_t String2Uint64(const string &value) {
229  uint64_t result;
230  sscanf(value.c_str(), "%" PRIu64, &result);
231  return result;
232 }
233 
243 bool String2Uint64Parse(const std::string &value, uint64_t *result) {
244  char *endptr = NULL;
245  errno = 0;
246  long long myval = strtoll(value.c_str(), &endptr, 10); // NOLINT
247  if ((value.size() == 0) || (endptr != (value.c_str() + value.size())) ||
248  (myval < 0)) {
249  errno = EINVAL;
250  return false;
251  }
252  if (errno) {
253  return false;
254  }
255  if (result) {
256  *result = myval;
257  }
258  return true;
259 }
260 
261 void String2Uint64Pair(const string &value, uint64_t *a, uint64_t *b) {
262  sscanf(value.c_str(), "%" PRIu64 " %" PRIu64, a, b);
263 }
264 
265 bool HasPrefix(const string &str, const string &prefix,
266  const bool ignore_case) {
267  if (prefix.length() > str.length()) return false;
268 
269  for (unsigned i = 0, l = prefix.length(); i < l; ++i) {
270  if (ignore_case) {
271  if (toupper(str[i]) != toupper(prefix[i])) return false;
272  } else {
273  if (str[i] != prefix[i]) return false;
274  }
275  }
276  return true;
277 }
278 
279 bool HasSuffix(const std::string &str, const std::string &suffix,
280  const bool ignore_case) {
281  if (suffix.size() > str.size()) return false;
282  const IgnoreCaseComperator icmp;
283  return (ignore_case)
284  ? std::equal(suffix.rbegin(), suffix.rend(), str.rbegin(), icmp)
285  : std::equal(suffix.rbegin(), suffix.rend(), str.rbegin());
286 }
287 
288 vector<string> SplitString(const string &str, const char delim,
289  const unsigned max_chunks) {
290  vector<string> result;
291 
292  // edge case... one chunk is always the whole string
293  if (1 == max_chunks) {
294  result.push_back(str);
295  return result;
296  }
297 
298  // split the string
299  const unsigned size = str.size();
300  unsigned marker = 0;
301  unsigned chunks = 1;
302  unsigned i;
303  for (i = 0; i < size; ++i) {
304  if (str[i] == delim) {
305  result.push_back(str.substr(marker, i - marker));
306  marker = i + 1;
307 
308  // we got what we want... good bye
309  if (++chunks == max_chunks) break;
310  }
311  }
312 
313  // push the remainings of the string and return
314  result.push_back(str.substr(marker));
315  return result;
316 }
317 
318 string JoinStrings(const vector<string> &strings, const string &joint) {
319  string result = "";
320  const unsigned size = strings.size();
321 
322  if (size > 0) {
323  result = strings[0];
324  for (unsigned i = 1; i < size; ++i) result += joint + strings[i];
325  }
326 
327  return result;
328 }
329 
330 void ParseKeyvalMem(const unsigned char *buffer, const unsigned buffer_size,
331  map<char, string> *content) {
332  string line;
333  unsigned pos = 0;
334  while (pos < buffer_size) {
335  if (static_cast<char>(buffer[pos]) == '\n') {
336  if (line == "--") return;
337 
338  if (line != "") {
339  const string tail = (line.length() == 1) ? "" : line.substr(1);
340  // Special handling of 'Z' key because it can exist multiple times
341  if (line[0] != 'Z') {
342  (*content)[line[0]] = tail;
343  } else {
344  if (content->find(line[0]) == content->end()) {
345  (*content)[line[0]] = tail;
346  } else {
347  (*content)[line[0]] = (*content)[line[0]] + "|" + tail;
348  }
349  }
350  }
351  line = "";
352  } else {
353  line += static_cast<char>(buffer[pos]);
354  }
355  pos++;
356  }
357 }
358 
359 bool ParseKeyvalPath(const string &filename, map<char, string> *content) {
360  int fd = open(filename.c_str(), O_RDONLY);
361  if (fd < 0) return false;
362 
363  unsigned char buffer[4096];
364  ssize_t num_bytes = read(fd, buffer, sizeof(buffer));
365  close(fd);
366 
367  if ((num_bytes <= 0) || (unsigned(num_bytes) >= sizeof(buffer))) return false;
368 
369  ParseKeyvalMem(buffer, unsigned(num_bytes), content);
370  return true;
371 }
372 
373 string GetLineMem(const char *text, const int text_size) {
374  int pos = 0;
375  while ((pos < text_size) && (text[pos] != '\n')) pos++;
376  return string(text, pos);
377 }
378 
379 bool GetLineFile(FILE *f, std::string *line) {
380  int retval;
381  line->clear();
382  while (true) {
383  retval = fgetc(f);
384  if (ferror(f) && (errno == EINTR)) {
385  clearerr(f);
386  continue;
387  } else if (retval == EOF) {
388  break;
389  }
390  char c = static_cast<char>(retval);
391  if (c == '\n') break;
392  line->push_back(c);
393  }
394  return (retval != EOF) || !line->empty();
395 }
396 
397 bool GetLineFd(const int fd, std::string *line) {
398  ssize_t retval;
399  char c;
400  line->clear();
401  while (true) {
402  retval = read(fd, &c, 1);
403  if (retval == 0) {
404  break;
405  }
406  if ((retval == -1) && (errno == EINTR)) {
407  continue;
408  }
409  if (retval == -1) {
410  break;
411  }
412  if (c == '\n') break;
413  line->push_back(c);
414  }
415  return (retval == 1) || !line->empty();
416 }
417 
421 string Trim(const string &raw, bool trim_newline) {
422  if (raw.empty()) return "";
423 
424  unsigned start_pos = 0;
425  for (; (start_pos < raw.length()) &&
426  (raw[start_pos] == ' ' || raw[start_pos] == '\t' ||
427  (trim_newline && (raw[start_pos] == '\n' || raw[start_pos] == '\r')));
428  ++start_pos)
429  {
430  }
431  unsigned end_pos = raw.length() - 1; // at least one character in raw
432  for (;
433  (end_pos >= start_pos) &&
434  (raw[end_pos] == ' ' || raw[end_pos] == '\t' ||
435  (trim_newline && (raw[end_pos] == '\n' || raw[end_pos] == '\r')));
436  --end_pos)
437  {
438  }
439 
440  return raw.substr(start_pos, end_pos - start_pos + 1);
441 }
442 
446 string ToUpper(const string &mixed_case) {
447  string result(mixed_case);
448  for (unsigned i = 0, l = result.length(); i < l; ++i) {
449  result[i] = static_cast<char>(toupper(result[i]));
450  }
451  return result;
452 }
453 
454 string ReplaceAll(const string &haystack, const string &needle,
455  const string &replace_by) {
456  string result(haystack);
457  size_t pos = 0;
458  const unsigned needle_size = needle.size();
459  if (needle == "") return result;
460 
461  while ((pos = result.find(needle, pos)) != string::npos)
462  result.replace(pos, needle_size, replace_by);
463  return result;
464 }
465 
466 static inline void Base64Block(const unsigned char input[3], const char *table,
467  char output[4]) {
468  output[0] = table[(input[0] & 0xFD) >> 2];
469  output[1] = table[((input[0] & 0x03) << 4) | ((input[1] & 0xF0) >> 4)];
470  output[2] = table[((input[1] & 0x0F) << 2) | ((input[2] & 0xD0) >> 6)];
471  output[3] = table[input[2] & 0x3F];
472 }
473 
474 string Base64(const string &data) {
475  string result;
476  result.reserve((data.length() + 3) * 4 / 3);
477  unsigned pos = 0;
478  const unsigned char *data_ptr =
479  reinterpret_cast<const unsigned char *>(data.data());
480  const unsigned length = data.length();
481  while (pos + 2 < length) {
482  char encoded_block[4];
483  Base64Block(data_ptr + pos, b64_table, encoded_block);
484  result.append(encoded_block, 4);
485  pos += 3;
486  }
487  if (length % 3 != 0) {
488  unsigned char input[3];
489  input[0] = data_ptr[pos];
490  input[1] = ((length % 3) == 2) ? data_ptr[pos + 1] : 0;
491  input[2] = 0;
492  char encoded_block[4];
493  Base64Block(input, b64_table, encoded_block);
494  result.append(encoded_block, 2);
495  result.push_back(((length % 3) == 2) ? encoded_block[2] : '=');
496  result.push_back('=');
497  }
498 
499  return result;
500 }
501 
505 string Base64Url(const string &data) {
506  string base64 = Base64(data);
507  for (unsigned i = 0, l = base64.length(); i < l; ++i) {
508  if (base64[i] == '+') {
509  base64[i] = '-';
510  } else if (base64[i] == '/') {
511  base64[i] = '_';
512  }
513  }
514  return base64;
515 }
516 
517 static bool Debase64Block(const unsigned char input[4], unsigned char output[3])
518 {
519  int32_t dec[4];
520  for (int i = 0; i < 4; ++i) {
521  dec[i] = db64_table[input[i]];
522  if (dec[i] < 0) return false;
523  }
524 
525  output[0] = (dec[0] << 2) | (dec[1] >> 4);
526  output[1] = ((dec[1] & 0x0F) << 4) | (dec[2] >> 2);
527  output[2] = ((dec[2] & 0x03) << 6) | dec[3];
528  return true;
529 }
530 
534 bool Debase64(const string &data, string *decoded) {
535  decoded->clear();
536  decoded->reserve((data.length() + 4) * 3 / 4);
537  unsigned pos = 0;
538  const unsigned char *data_ptr =
539  reinterpret_cast<const unsigned char *>(data.data());
540  const unsigned length = data.length();
541  if (length == 0) return true;
542  if ((length % 4) != 0) return false;
543 
544  while (pos < length) {
545  unsigned char decoded_block[3];
546  bool retval = Debase64Block(data_ptr + pos, decoded_block);
547  if (!retval) return false;
548  decoded->append(reinterpret_cast<char *>(decoded_block), 3);
549  pos += 4;
550  }
551 
552  for (int i = 0; i < 2; ++i) {
553  pos--;
554  if (data[pos] == '=') decoded->erase(decoded->length() - 1);
555  }
556  return true;
557 }
558 
562 string Tail(const string &source, unsigned num_lines) {
563  if (source.empty() || (num_lines == 0)) return "";
564 
565  int l = static_cast<int>(source.length());
566  int i = l - 1;
567  for (; i >= 0; --i) {
568  char c = source.data()[i];
569  if (c == '\n') {
570  if (num_lines == 0) {
571  return source.substr(i + 1);
572  }
573  num_lines--;
574  }
575  }
576  return source;
577 }
578 
585 std::string GetGMTimestamp(const std::string &format) {
586  struct tm time_ptr;
587  char date_and_time[100];
588  time_t t = time(NULL);
589  gmtime_r(&t, &time_ptr); // take UTC
590  // return empty string if formatting fails
591  if (!strftime(date_and_time, 100, format.c_str(), &time_ptr)) {
592  return "";
593  }
594  std::string timestamp(date_and_time);
595  return timestamp;
596 }
597 
598 #ifdef CVMFS_NAMESPACE_GUARD
599 } // namespace CVMFS_NAMESPACE_GUARD
600 #endif
string GetLineMem(const char *text, const int text_size)
Definition: string.cc:373
std::string GetGMTimestamp(const std::string &format)
Definition: string.cc:585
vector< string > SplitString(const string &str, const char delim, const unsigned max_chunks)
Definition: string.cc:288
std::string IsoTimestamp()
Definition: string.cc:149
string Trim(const string &raw, bool trim_newline)
Definition: string.cc:421
string ReplaceAll(const string &haystack, const string &needle, const string &replace_by)
Definition: string.cc:454
static void Base64Block(const unsigned char input[3], const char *table, char output[4])
Definition: string.cc:466
string JoinStrings(const vector< string > &strings, const string &joint)
Definition: string.cc:318
string Tail(const string &source, unsigned num_lines)
Definition: string.cc:562
string StringifyTime(const time_t seconds, const bool utc)
Definition: string.cc:105
string StringifyDouble(const double value)
Definition: string.cc:96
std::string StringifyUint(const uint64_t value)
Definition: string.cc:84
bool Debase64(const string &data, string *decoded)
Definition: string.cc:534
string StringifyByteAsHex(const unsigned char value)
Definition: string.cc:90
string StringifyBool(const bool value)
Definition: string.cc:76
bool String2Uint64Parse(const std::string &value, uint64_t *result)
Definition: string.cc:243
string Base64Url(const string &data)
Definition: string.cc:505
std::string RfcTimestamp()
Definition: string.cc:127
int64_t String2Int64(const string &value)
Definition: string.cc:222
bool GetLineFile(FILE *f, std::string *line)
Definition: string.cc:379
string ToUpper(const string &mixed_case)
Definition: string.cc:446
bool HasSuffix(const std::string &str, const std::string &suffix, const bool ignore_case)
Definition: string.cc:279
std::string WhitelistTimestamp(time_t when)
Definition: string.cc:169
string StringifyInt(const int64_t value)
Definition: string.cc:78
bool HasPrefix(const string &str, const string &prefix, const bool ignore_case)
Definition: string.cc:265
time_t IsoTimestamp2UtcTime(const std::string &iso8601)
Definition: string.cc:198
bool GetLineFd(const int fd, std::string *line)
Definition: string.cc:397
string Base64(const string &data)
Definition: string.cc:474
uint64_t String2Uint64(const string &value)
Definition: string.cc:228
bool operator()(const std::string::value_type a, const std::string::value_type b) const
Definition: string.cc:68
bool ParseKeyvalPath(const string &filename, map< char, string > *content)
Definition: string.cc:359
static bool Debase64Block(const unsigned char input[4], unsigned char output[3])
Definition: string.cc:517
const char b64_table[]
Definition: string.cc:33
string StringifyTimeval(const timeval value)
Definition: string.cc:185
const int8_t db64_table[]
Definition: string.cc:43
static void size_t size
Definition: smalloc.h:47
void ParseKeyvalMem(const unsigned char *buffer, const unsigned buffer_size, map< char, string > *content)
Definition: string.cc:330
void String2Uint64Pair(const string &value, uint64_t *a, uint64_t *b)
Definition: string.cc:261