]> git.proxmox.com Git - mirror_smartmontools-debian.git/blobdiff - utility.cpp
import smartmontools 7.0
[mirror_smartmontools-debian.git] / utility.cpp
index 36163c7e4a72def0d4dca0fe4f7896eaddb1e730..e83f6304d23ec74f09e838e0e6f74ca2fdaa8327 100644 (file)
@@ -1,57 +1,49 @@
 /*
  * utility.cpp
  *
- * Home page of code is: http://smartmontools.sourceforge.net
+ * Home page of code is: http://www.smartmontools.org
  *
- * Copyright (C) 2002-10 Bruce Allen <smartmontools-support@lists.sourceforge.net>
- * Copyright (C) 2008-10 Christian Franke <smartmontools-support@lists.sourceforge.net>
+ * Copyright (C) 2002-12 Bruce Allen
+ * Copyright (C) 2008-18 Christian Franke
  * Copyright (C) 2000 Michael Cornwell <cornwell@acm.org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2, or (at your option)
- * any later version.
- *
- * You should have received a copy of the GNU General Public License
- * (for example COPYING); if not, write to the Free
- * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * This code was originally developed as a Senior Thesis by Michael Cornwell
- * at the Concurrent Systems Laboratory (now part of the Storage Systems
- * Research Center), Jack Baskin School of Engineering, University of
- * California, Santa Cruz. http://ssrc.soe.ucsc.edu/
- *
+ * SPDX-License-Identifier: GPL-2.0-or-later
  */
 
 // THIS FILE IS INTENDED FOR UTILITY ROUTINES THAT ARE APPLICABLE TO
 // BOTH SCSI AND ATA DEVICES, AND THAT MAY BE USED IN SMARTD,
 // SMARTCTL, OR BOTH.
 
+#include "config.h"
+#define __STDC_FORMAT_MACROS 1 // enable PRI* for C++
+
+#include <inttypes.h>
 #include <stdio.h>
 #include <string.h>
 #include <time.h>
 #include <errno.h>
 #include <stdlib.h>
 #include <ctype.h>
-#include <syslog.h>
 #include <stdarg.h>
 #include <sys/stat.h>
+#ifdef HAVE_LOCALE_H
+#include <locale.h>
+#endif
 #ifdef _WIN32
 #include <mbstring.h> // _mbsinc()
 #endif
 
 #include <stdexcept>
 
-#include "config.h"
 #include "svnversion.h"
-#include "int64.h"
 #include "utility.h"
 
 #include "atacmds.h"
 #include "dev_interface.h"
+#include "sg_unaligned.h"
 
-const char * utility_cpp_cvsid = "$Id: utility.cpp 3046 2010-01-22 21:30:02Z chrfranke $"
-                                 UTILITY_H_CVSID INT64_H_CVSID;
+const char * utility_cpp_cvsid = "$Id: utility.cpp 4842 2018-12-02 16:07:26Z chrfranke $"
+  UTILITY_H_CVSID;
 
 const char * packet_types[] = {
         "Direct-access (disk)",
@@ -72,13 +64,6 @@ const char * packet_types[] = {
         "Optical card reader/writer"
 };
 
-// Whenever exit() status is EXIT_BADCODE, please print this message
-const char *reportbug="Please report this bug to the Smartmontools developers at " PACKAGE_BUGREPORT ".\n";
-
-
-// command-line argument: are we running in debug mode?.
-unsigned char debugmode = 0;
-
 // BUILD_INFO can be provided by package maintainers
 #ifndef BUILD_INFO
 #define BUILD_INFO "(local build)"
@@ -88,36 +73,70 @@ unsigned char debugmode = 0;
 std::string format_version_info(const char * prog_name, bool full /*= false*/)
 {
   std::string info = strprintf(
-    "%s "PACKAGE_VERSION" "SMARTMONTOOLS_SVN_DATE" r"SMARTMONTOOLS_SVN_REV
-      " [%s] "BUILD_INFO"\n"
-    "Copyright (C) 2002-10 by Bruce Allen, http://smartmontools.sourceforge.net\n",
+    "%s " PACKAGE_VERSION " "
+#ifdef SMARTMONTOOLS_SVN_REV
+      SMARTMONTOOLS_SVN_DATE " r" SMARTMONTOOLS_SVN_REV
+#else
+      "(build date " __DATE__ ")" // checkout without expansion of Id keywords
+#endif
+      " [%s] " BUILD_INFO "\n"
+    "Copyright (C) 2002-18, Bruce Allen, Christian Franke, www.smartmontools.org\n",
     prog_name, smi()->get_os_version_str().c_str()
   );
   if (!full)
     return info;
 
-  info += strprintf(
-    "\n"
-    "%s comes with ABSOLUTELY NO WARRANTY. This is free\n"
+  info += "\n";
+  info += prog_name;
+  info += " comes with ABSOLUTELY NO WARRANTY. This is free\n"
     "software, and you are welcome to redistribute it under\n"
-    "the terms of the GNU General Public License Version 2.\n"
+    "the terms of the GNU General Public License; either\n"
+    "version 2, or (at your option) any later version.\n"
     "See http://www.gnu.org for further details.\n"
-    "\n",
-    prog_name
-  );
-  info += strprintf(
-    "smartmontools release "PACKAGE_VERSION
-      " dated "SMARTMONTOOLS_RELEASE_DATE" at "SMARTMONTOOLS_RELEASE_TIME"\n"
-    "smartmontools SVN rev "SMARTMONTOOLS_SVN_REV
-      " dated "SMARTMONTOOLS_SVN_DATE" at "SMARTMONTOOLS_SVN_TIME"\n"
-    "smartmontools build host: "SMARTMONTOOLS_BUILD_HOST"\n"
-    "smartmontools build configured: "SMARTMONTOOLS_CONFIGURE_DATE "\n"
-    "%s compile dated "__DATE__" at "__TIME__"\n"
-    "smartmontools configure arguments: ",
-    prog_name
-  );
+    "\n"
+    "smartmontools release " PACKAGE_VERSION
+      " dated " SMARTMONTOOLS_RELEASE_DATE " at " SMARTMONTOOLS_RELEASE_TIME "\n"
+#ifdef SMARTMONTOOLS_SVN_REV
+    "smartmontools SVN rev " SMARTMONTOOLS_SVN_REV
+      " dated " SMARTMONTOOLS_SVN_DATE " at " SMARTMONTOOLS_SVN_TIME "\n"
+#else
+    "smartmontools SVN rev is unknown\n"
+#endif
+    "smartmontools build host: " SMARTMONTOOLS_BUILD_HOST "\n"
+    "smartmontools build with: "
+
+#define N2S_(s) #s
+#define N2S(s) "(" N2S_(s) ")"
+#if   __cplusplus >  201703
+                               "C++2x" N2S(__cplusplus)
+#elif __cplusplus == 201703
+                               "C++17"
+#elif __cplusplus >  201402
+                               "C++14" N2S(__cplusplus)
+#elif __cplusplus == 201402
+                               "C++14"
+#elif __cplusplus >  201103
+                               "C++11" N2S(__cplusplus)
+#elif __cplusplus == 201103
+                               "C++11"
+#elif __cplusplus >  199711
+                               "C++98" N2S(__cplusplus)
+#elif __cplusplus == 199711
+                               "C++98"
+#else
+                               "C++"   N2S(__cplusplus)
+#endif
+#undef N2S
+#undef N2S_
+
+#if defined(__GNUC__) && defined(__VERSION__) // works also with CLang
+                                     ", GCC " __VERSION__
+#endif
+                                                          "\n"
+    "smartmontools configure arguments:"
+  ;
   info += (sizeof(SMARTMONTOOLS_CONFIGURE_ARGS) > 1 ?
-           SMARTMONTOOLS_CONFIGURE_ARGS : "[no arguments given]");
+           SMARTMONTOOLS_CONFIGURE_ARGS : " [no arguments given]");
   info += '\n';
 
   return info;
@@ -150,7 +169,7 @@ static char *ReadSiteDefaultTimezone(){
 #endif
 
 // Make sure that this executable is aware if the user has changed the
-// time-zone since the last time we polled devices. The cannonical
+// time-zone since the last time we polled devices. The canonical
 // example is a user who starts smartd on a laptop, then flies across
 // time-zones with a laptop, and then changes the timezone, WITHOUT
 // restarting smartd. This is a work-around for a bug in
@@ -259,28 +278,15 @@ const char *packetdevicetype(int type){
   return "Unknown";
 }
 
-
-// Returns 1 if machine is big endian, else zero.  This is a run-time
-// rather than a compile-time function.  We could do it at
-// compile-time but in principle there are architectures that can run
-// with either byte-ordering.
-int isbigendian(){
-  short i=0x0100;
-  char *tmp=(char *)&i;
-  return *tmp;
-}
-
 // Utility function prints date and time and timezone into a character
-// buffer of length>=64.  All the fuss is needed to get the right
+// buffer of length 64.  All the fuss is needed to get the right
 // timezone info (sigh).
-void dateandtimezoneepoch(char *buffer, time_t tval){
+void dateandtimezoneepoch(char (& buffer)[DATEANDEPOCHLEN], time_t tval)
+{
   struct tm *tmval;
   const char *timezonename;
   char datebuffer[DATEANDEPOCHLEN];
   int lenm1;
-#ifdef _WIN32
-  char tzfixbuf[6+1];
-#endif
 
   FixGlibcTimeZoneBug();
   
@@ -296,7 +302,12 @@ void dateandtimezoneepoch(char *buffer, time_t tval){
   // Remove newline
   lenm1=strlen(datebuffer)-1;
   datebuffer[lenm1>=0?lenm1:0]='\0';
-  
+
+#if defined(_WIN32) && defined(_MSC_VER)
+  // tzname is missing in MSVC14
+  #define tzname _tzname
+#endif
+
   // correct timezone name
   if (tmval->tm_isdst==0)
     // standard time zone
@@ -310,6 +321,8 @@ void dateandtimezoneepoch(char *buffer, time_t tval){
 
 #ifdef _WIN32
   // Fix long non-ascii timezone names
+    // cppcheck-suppress variableScope
+  char tzfixbuf[6+1] = "";
   if (!getenv("TZ"))
     timezonename=fixtzname(tzfixbuf, sizeof(tzfixbuf), timezonename);
 #endif
@@ -320,16 +333,6 @@ void dateandtimezoneepoch(char *buffer, time_t tval){
   return;
 }
 
-// Date and timezone gets printed into string pointed to by buffer
-void dateandtimezone(char *buffer){
-  
-  // Get the epoch (time in seconds since Jan 1 1970)
-  time_t tval=time(NULL);
-  
-  dateandtimezoneepoch(buffer, tval);
-  return;
-}
-
 // A replacement for perror() that sends output to our choice of
 // printing. If errno not set then just print message.
 void syserror(const char *message){
@@ -351,41 +354,71 @@ void syserror(const char *message){
   return;
 }
 
+// Check regular expression for non-portable features.
+//
 // POSIX extended regular expressions interpret unmatched ')' ordinary:
 // "The close-parenthesis shall be considered special in this context
 //  only if matched with a preceding open-parenthesis."
 //
-// Actual '(...)' nesting errors remain undetected on strict POSIX
-// implementations (glibc) but an error is reported on others (Cygwin).
-// 
-// The check below is rather incomplete because it does not handle
-// e.g. '\)' '[)]'.
-// But it should work for the regex subset used in drive database
-// and smartd '-s' directives.
-static int check_regex_nesting(const char * pattern)
+// GNU libc and BSD libc support unmatched ')', Cygwin reports an error.
+//
+// POSIX extended regular expressions do not define empty subexpressions:
+// "A vertical-line appearing first or last in an ERE, or immediately following
+//  a vertical-line or a left-parenthesis, or immediately preceding a
+//  right-parenthesis, produces undefined results."
+//
+// GNU libc and Cygwin support empty subexpressions, BSD libc reports an error.
+//
+static const char * check_regex(const char * pattern)
 {
-  int level = 0, i;
-  for (i = 0; pattern[i] && level >= 0; i++) {
-    switch (pattern[i]) {
-      case '(': level++; break;
-      case ')': level--; break;
+  int level = 0;
+  char c;
+
+  for (int i = 0; (c = pattern[i]); i++) {
+    // Skip "\x"
+    if (c == '\\') {
+      if (!pattern[++i])
+        break;
+      continue;
     }
+
+    // Skip "[...]"
+    if (c == '[') {
+      if (pattern[++i] == '^')
+        i++;
+      if (!pattern[i++])
+        break;
+      while ((c = pattern[i]) && c != ']')
+        i++;
+      if (!c)
+        break;
+      continue;
+    }
+
+    // Check "(...)" nesting
+    if (c == '(')
+      level++;
+    else if (c == ')' && --level < 0)
+      return "Unmatched ')'";
+
+    // Check for leading/trailing '|' or "||", "|)", "|$", "(|", "^|"
+    char c1;
+    if (   (c == '|' && (   i == 0 || !(c1 = pattern[i+1])
+                          || c1 == '|' || c1 == ')' || c1 == '$'))
+        || ((c == '(' || c == '^') && pattern[i+1] == '|')       )
+      return "Empty '|' subexpression";
   }
-  return level;
+
+  return (const char *)0;
 }
 
-// Wrapper class for regex(3)
+// Wrapper class for POSIX regex(3) or std::regex
 
-regular_expression::regular_expression()
-: m_flags(0)
-{
-  memset(&m_regex_buf, 0, sizeof(m_regex_buf));
-}
+#ifndef WITH_CXX11_REGEX
 
-regular_expression::regular_expression(const char * pattern, int flags)
+regular_expression::regular_expression()
 {
   memset(&m_regex_buf, 0, sizeof(m_regex_buf));
-  compile(pattern, flags);
 }
 
 regular_expression::~regular_expression()
@@ -394,15 +427,19 @@ regular_expression::~regular_expression()
 }
 
 regular_expression::regular_expression(const regular_expression & x)
+: m_pattern(x.m_pattern),
+  m_errmsg(x.m_errmsg)
 {
   memset(&m_regex_buf, 0, sizeof(m_regex_buf));
-  copy(x);
+  copy_buf(x);
 }
 
 regular_expression & regular_expression::operator=(const regular_expression & x)
 {
+  m_pattern = x.m_pattern;
+  m_errmsg = x.m_errmsg;
   free_buf();
-  copy(x);
+  copy_buf(x);
   return *this;
 }
 
@@ -414,13 +451,9 @@ void regular_expression::free_buf()
   }
 }
 
-void regular_expression::copy(const regular_expression & x)
+void regular_expression::copy_buf(const regular_expression & x)
 {
-  m_pattern = x.m_pattern;
-  m_flags = x.m_flags;
-  m_errmsg = x.m_errmsg;
-
-  if (!m_pattern.empty() && m_errmsg.empty()) {
+  if (nonempty(&x.m_regex_buf, sizeof(x.m_regex_buf))) {
     // There is no POSIX compiled-regex-copy command.
     if (!compile())
       throw std::runtime_error(strprintf(
@@ -429,17 +462,39 @@ void regular_expression::copy(const regular_expression & x)
   }
 }
 
-bool regular_expression::compile(const char * pattern, int flags)
+#endif // !WITH_CXX11_REGEX
+
+regular_expression::regular_expression(const char * pattern)
+: m_pattern(pattern)
+{
+  if (!compile())
+    throw std::runtime_error(strprintf(
+      "error in regular expression \"%s\": %s",
+      m_pattern.c_str(), m_errmsg.c_str()));
+}
+
+bool regular_expression::compile(const char * pattern)
 {
+#ifndef WITH_CXX11_REGEX
   free_buf();
+#endif
   m_pattern = pattern;
-  m_flags = flags;
   return compile();
 }
 
 bool regular_expression::compile()
 {
-  int errcode = regcomp(&m_regex_buf, m_pattern.c_str(), m_flags);
+#ifdef WITH_CXX11_REGEX
+  try {
+    m_regex.assign(m_pattern, std::regex_constants::extended);
+  }
+  catch (std::regex_error & ex) {
+    m_errmsg = ex.what();
+    return false;
+  }
+
+#else
+  int errcode = regcomp(&m_regex_buf, m_pattern.c_str(), REG_EXTENDED);
   if (errcode) {
     char errmsg[512];
     regerror(errcode, &m_regex_buf, errmsg, sizeof(errmsg));
@@ -447,10 +502,16 @@ bool regular_expression::compile()
     free_buf();
     return false;
   }
+#endif
 
-  if (check_regex_nesting(m_pattern.c_str()) < 0) {
-    m_errmsg = "Unmatched ')'";
+  const char * errmsg = check_regex(m_pattern.c_str());
+  if (errmsg) {
+    m_errmsg = errmsg;
+#ifdef WITH_CXX11_REGEX
+    m_regex = std::regex();
+#else
     free_buf();
+#endif
     return false;
   }
 
@@ -458,104 +519,38 @@ bool regular_expression::compile()
   return true;
 }
 
-// Splits an argument to the -r option into a name part and an (optional) 
-// positive integer part.  s is a pointer to a string containing the
-// argument.  After the call, s will point to the name part and *i the
-// integer part if there is one or 1 otherwise.  Note that the string s may
-// be changed by this function.  Returns zero if successful and non-zero
-// otherwise.
-int split_report_arg(char *s, int *i)
+bool regular_expression::full_match(const char * str) const
 {
-  if ((s = strchr(s, ','))) {
-    // Looks like there's a name part and an integer part.
-    char *tailptr;
-
-    *s++ = '\0';
-    if (*s == '0' || !isdigit((int)*s))  // The integer part must be positive
-      return 1;
-    errno = 0;
-    *i = (int) strtol(s, &tailptr, 10);
-    if (errno || *tailptr != '\0')
-      return 1;
-  } else {
-    // There's no integer part.
-    *i = 1;
-  }
-
-  return 0;
-}
-
-// same as above but sets *i to -1 if missing , argument
-int split_report_arg2(char *s, int *i){
-  char *tailptr;
-  s+=6;
-
-  if (*s=='\0' || !isdigit((int)*s)) { 
-    // What's left must be integer
-    *i=-1;
-    return 1;
-  }
-
-  errno = 0;
-  *i = (int) strtol(s, &tailptr, 10);
-  if (errno || *tailptr != '\0') {
-    *i=-1;
-    return 1;
-  }
-
-  return 0;
+#ifdef WITH_CXX11_REGEX
+  return std::regex_match(str, m_regex);
+#else
+  match_range range;
+  return (   !regexec(&m_regex_buf, str, 1, &range, 0)
+          && range.rm_so == 0 && range.rm_eo == (int)strlen(str));
+#endif
 }
 
-#ifndef HAVE_STRTOULL
-// Replacement for missing strtoull() (Linux with libc < 6, MSVC)
-// Functionality reduced to requirements of smartd and split_selective_arg().
-
-uint64_t strtoull(const char * p, char * * endp, int base)
+bool regular_expression::execute(const char * str, unsigned nmatch, match_range * pmatch) const
 {
-  uint64_t result, maxres;
-  int i = 0;
-  char c = p[i++];
-
-  if (!base) {
-    if (c == '0') {
-      if (p[i] == 'x' || p[i] == 'X') {
-        base = 16; i++;
-      }
-      else
-        base = 8;
-      c = p[i++];
+#ifdef WITH_CXX11_REGEX
+  std::cmatch m;
+  if (!std::regex_search(str, m, m_regex))
+    return false;
+  unsigned sz = m.size();
+  for (unsigned i = 0; i < nmatch; i++) {
+    if (i < sz && *m[i].first) {
+      pmatch[i].rm_so = m[i].first  - str;
+      pmatch[i].rm_eo = m[i].second - str;
     }
     else
-      base = 10;
+      pmatch[i].rm_so = pmatch[i].rm_eo = -1;
   }
+  return true;
 
-  result = 0;
-  maxres = ~(uint64_t)0 / (unsigned)base;
-  for (;;) {
-    unsigned digit;
-    if ('0' <= c && c <= '9')
-      digit = c - '0';
-    else if ('A' <= c && c <= 'Z')
-      digit = c - 'A' + 10;
-    else if ('a' <= c && c <= 'z')
-      digit = c - 'a' + 10;
-    else
-      break;
-    if (digit >= (unsigned)base)
-      break;
-    if (!(   result < maxres
-          || (result == maxres && digit <= ~(uint64_t)0 % (unsigned)base))) {
-      result = ~(uint64_t)0; errno = ERANGE; // return on overflow
-      break;
-    }
-    result = result * (unsigned)base + digit;
-    c = p[i++];
-  }
-  if (endp)
-    *endp = (char *)p + i - 1;
-  return result;
+#else
+  return !regexec(&m_regex_buf, str, nmatch, pmatch, 0);
+#endif
 }
-#endif // HAVE_STRTOLL
 
 // Splits an argument to the -t option that is assumed to be of the form
 // "selective,%lld-%lld" (prefixes of "0" (for octal) and "0x"/"0X" (for hex)
@@ -599,6 +594,8 @@ int split_selective_arg(char *s, uint64_t *start,
       return 0;
     }
   }
+
+  errno = 0;
   *stop = strtoull(s+1, &tailptr, 0);
   if (errno || *tailptr != '\0')
     return 1;
@@ -610,85 +607,6 @@ int split_selective_arg(char *s, uint64_t *start,
   return 0;
 }
 
-#ifdef OLD_INTERFACE
-
-// smartd exit codes
-#define EXIT_NOMEM     8   // out of memory
-#define EXIT_BADCODE   10  // internal error - should NEVER happen
-
-int64_t bytes = 0;
-
-// Helps debugging.  If the second argument is non-negative, then
-// decrement bytes by that amount.  Else decrement bytes by (one plus)
-// length of null terminated string.
-void *FreeNonZero1(void *address, int size, int line, const char* file){
-  if (address) {
-    if (size<0)
-      bytes-=1+strlen((char*)address);
-    else
-      bytes-=size;
-    return CheckFree1(address, line, file);
-  }
-  return NULL;
-}
-
-// To help with memory checking.  Use when it is known that address is
-// NOT null.
-void *CheckFree1(void *address, int whatline, const char* file){
-  if (address){
-    free(address);
-    return NULL;
-  }
-  
-  PrintOut(LOG_CRIT, "Internal error in CheckFree() at line %d of file %s\n%s", 
-           whatline, file, reportbug);
-  EXIT(EXIT_BADCODE);
-}
-
-// A custom version of calloc() that tracks memory use
-void *Calloc(size_t nmemb, size_t size) { 
-  void *ptr=calloc(nmemb, size);
-  
-  if (ptr)
-    bytes+=nmemb*size;
-
-  return ptr;
-}
-
-// A custom version of strdup() that keeps track of how much memory is
-// being allocated. If mustexist is set, it also throws an error if we
-// try to duplicate a NULL string.
-char *CustomStrDup(const char *ptr, int mustexist, int whatline, const char* file){
-  char *tmp;
-
-  // report error if ptr is NULL and mustexist is set
-  if (ptr==NULL){
-    if (mustexist) {
-      PrintOut(LOG_CRIT, "Internal error in CustomStrDup() at line %d of file %s\n%s", 
-               whatline, file, reportbug);
-      EXIT(EXIT_BADCODE);
-    }
-    else
-      return NULL;
-  }
-
-  // make a copy of the string...
-  tmp=strdup(ptr);
-  
-  if (!tmp) {
-    PrintOut(LOG_CRIT, "No memory to duplicate string %s at line %d of file %s\n", ptr, whatline, file);
-    EXIT(EXIT_NOMEM);
-  }
-  
-  // and track memory usage
-  bytes+=1+strlen(ptr);
-  
-  return tmp;
-}
-
-#endif // OLD_INTERFACE
-
-
 // Returns true if region of memory contains non-zero entries
 bool nonempty(const void * data, int size)
 {
@@ -698,37 +616,107 @@ bool nonempty(const void * data, int size)
   return false;
 }
 
+// Copy not null terminated char array to null terminated string.
+// Replace non-ascii characters.  Remove leading and trailing blanks.
+const char * format_char_array(char * str, int strsize, const char * chr, int chrsize)
+{
+  int b = 0;
+  while (b < chrsize && chr[b] == ' ')
+    b++;
+  int n = 0;
+  while (b+n < chrsize && chr[b+n])
+    n++;
+  while (n > 0 && chr[b+n-1] == ' ')
+    n--;
+
+  if (n >= strsize)
+    n = strsize-1;
+
+  for (int i = 0; i < n; i++) {
+    char c = chr[b+i];
+    str[i] = (' ' <= c && c <= '~' ? c : '?');
+  }
 
-// This routine converts an integer number of milliseconds into a test
-// string of the form Xd+Yh+Zm+Ts.msec.  The resulting text string is
-// written to the array.
-void MsecToText(unsigned int msec, char *txt){
-  int start=0;
-  unsigned int days, hours, min, sec;
+  str[n] = 0;
+  return str;
+}
 
-  days       = msec/86400000U;
-  msec      -= days*86400000U;
+// Format integer with thousands separator
+const char * format_with_thousands_sep(char * str, int strsize, uint64_t val,
+                                       const char * thousands_sep /* = 0 */)
+{
+  if (!thousands_sep) {
+    thousands_sep = ",";
+#ifdef HAVE_LOCALE_H
+    setlocale(LC_ALL, "");
+    const struct lconv * currentlocale = localeconv();
+    if (*(currentlocale->thousands_sep))
+      thousands_sep = currentlocale->thousands_sep;
+#endif
+  }
+
+  char num[64];
+  snprintf(num, sizeof(num), "%" PRIu64, val);
+  int numlen = strlen(num);
+
+  int i = 0, j = 0;
+  do
+    str[j++] = num[i++];
+  while (i < numlen && (numlen - i) % 3 != 0 && j < strsize-1);
+  str[j] = 0;
+
+  while (i < numlen && j < strsize-1) {
+    j += snprintf(str+j, strsize-j, "%s%.3s", thousands_sep, num+i);
+    i += 3;
+  }
 
-  hours      = msec/3600000U; 
-  msec      -= hours*3600000U;
+  return str;
+}
 
-  min        = msec/60000U;
-  msec      -= min*60000U;
+// Format capacity with SI prefixes
+const char * format_capacity(char * str, int strsize, uint64_t val,
+                             const char * decimal_point /* = 0 */)
+{
+  if (!decimal_point) {
+    decimal_point = ".";
+#ifdef HAVE_LOCALE_H
+    setlocale(LC_ALL, "");
+    const struct lconv * currentlocale = localeconv();
+    if (*(currentlocale->decimal_point))
+      decimal_point = currentlocale->decimal_point;
+#endif
+  }
 
-  sec        = msec/1000U;
-  msec      -= sec*1000U;
+  const unsigned factor = 1000; // 1024 for KiB,MiB,...
+  static const char prefixes[] = " KMGTP";
 
-  if (days) {
-    txt += sprintf(txt, "%2dd+", (int)days);
-    start=1;
+  // Find d with val in [d, d*factor)
+  unsigned i = 0;
+  uint64_t d = 1;
+  for (uint64_t d2 = d * factor; val >= d2; d2 *= factor) {
+    d = d2;
+    if (++i >= sizeof(prefixes)-2)
+      break;
   }
 
-  sprintf(txt, "%02d:%02d:%02d.%03d", (int)hours, (int)min, (int)sec, (int)msec);  
-  return;
+  // Print 3 digits
+  uint64_t n = val / d;
+  if (i == 0)
+    snprintf(str, strsize, "%u B", (unsigned)n);
+  else if (n >= 100) // "123 xB"
+    snprintf(str, strsize, "%" PRIu64 " %cB", n, prefixes[i]);
+  else if (n >= 10)  // "12.3 xB"
+    snprintf(str, strsize, "%" PRIu64 "%s%u %cB", n, decimal_point,
+        (unsigned)(((val % d) * 10) / d), prefixes[i]);
+  else               // "1.23 xB"
+    snprintf(str, strsize, "%" PRIu64 "%s%02u %cB", n, decimal_point,
+        (unsigned)(((val % d) * 100) / d), prefixes[i]);
+
+  return str;
 }
 
 // return (v)sprintf() formatted std::string
-
+__attribute_format_printf(1, 0)
 std::string vstrprintf(const char * fmt, va_list ap)
 {
   char buf[512];
@@ -745,10 +733,87 @@ std::string strprintf(const char * fmt, ...)
   return str;
 }
 
+#if defined(HAVE___INT128)
+// Compiler supports '__int128'.
+
+// Recursive 128-bit to string conversion function
+static int snprint_uint128(char * str, int strsize, unsigned __int128 value)
+{
+  if (strsize <= 0)
+    return -1;
+
+  if (value <= 0xffffffffffffffffULL) {
+    // Print leading digits as 64-bit value
+    return snprintf(str, (size_t)strsize, "%" PRIu64, (uint64_t)value);
+  }
+  else {
+    // Recurse to print leading digits
+    const uint64_t e19 = 10000000000000000000ULL; // 2^63 < 10^19 < 2^64
+    int len1 = snprint_uint128(str, strsize, value / e19);
+    if (len1 < 0)
+      return -1;
+
+    // Print 19 digits remainder as 64-bit value
+    int len2 = snprintf(str + (len1 < strsize ? len1 : strsize - 1),
+                        (size_t)(len1 < strsize ? strsize - len1 : 1),
+                        "%019" PRIu64, (uint64_t)(value % e19)        );
+    if (len2 < 0)
+      return -1;
+    return len1 + len2;
+  }
+}
+
+// Convert 128-bit unsigned integer provided as two 64-bit halves to a string.
+const char * uint128_hilo_to_str(char * str, int strsize, uint64_t value_hi, uint64_t value_lo)
+{
+  snprint_uint128(str, strsize, ((unsigned __int128)value_hi << 64) | value_lo);
+  return str;
+}
+
+#elif defined(HAVE_LONG_DOUBLE_WIDER_PRINTF)
+// Compiler and *printf() support 'long double' which is wider than 'double'.
+
+const char * uint128_hilo_to_str(char * str, int strsize, uint64_t value_hi, uint64_t value_lo)
+{
+  snprintf(str, strsize, "%.0Lf", value_hi * (0xffffffffffffffffULL + 1.0L) + value_lo);
+  return str;
+}
+
+#else // !HAVE_LONG_DOUBLE_WIDER_PRINTF
+// No '__int128' or 'long double' support, use 'double'.
+
+const char * uint128_hilo_to_str(char * str, int strsize, uint64_t value_hi, uint64_t value_lo)
+{
+  snprintf(str, strsize, "%.0f", value_hi * (0xffffffffffffffffULL + 1.0) + value_lo);
+  return str;
+}
+
+#endif // HAVE___INT128
+
+// Runtime check of byte ordering, throws on error.
+static void check_endianness()
+{
+  const union {
+    // Force compile error if int type is not 32bit.
+    unsigned char c[sizeof(int) == 4 ? 8 : -1];
+    uint64_t i;
+  } x = {{1, 2, 3, 4, 5, 6, 7, 8}};
+  const uint64_t le = 0x0807060504030201ULL;
+  const uint64_t be = 0x0102030405060708ULL;
+
+  if (!(   x.i == (isbigendian() ? be : le)
+        && sg_get_unaligned_le16(x.c)   == (uint16_t)le
+        && sg_get_unaligned_be16(x.c+6) == (uint16_t)be
+        && sg_get_unaligned_le32(x.c)   == (uint32_t)le
+        && sg_get_unaligned_be32(x.c+4) == (uint32_t)be
+        && sg_get_unaligned_le64(x.c)   == le
+        && sg_get_unaligned_be64(x.c)   == be          ))
+    throw std::logic_error("CPU endianness does not match compile time test");
+}
 
 #ifndef HAVE_WORKING_SNPRINTF
-// Some versions of (v)snprintf() don't append null char on overflow (MSVCRT.DLL),
-// and/or return -1 on overflow (old Linux).
+// Some versions of (v)snprintf() don't append null char (MSVCRT.DLL),
+// and/or return -1 on output truncation (glibc <= 2.0.6).
 // Below are sane replacements substituted by #define in utility.h.
 
 #undef vsnprintf
@@ -777,4 +842,29 @@ int safe_snprintf(char *buf, int size, const char *fmt, ...)
   return i;
 }
 
-#endif
+static void check_snprintf() {}
+
+#elif defined(__GNUC__) && (__GNUC__ >= 7)
+
+// G++ 7+: Assume sane implementation and avoid -Wformat-truncation warning
+static void check_snprintf() {}
+
+#else
+
+static void check_snprintf()
+{
+  char buf[] =              "ABCDEFGHI";
+  int n1 = snprintf(buf, 8, "123456789");
+  int n2 = snprintf(buf, 0, "X");
+  if (!(!strcmp(buf, "1234567") && n1 == 9 && n2 == 1))
+    throw std::logic_error("Function snprintf() does not conform to C99");
+}
+
+#endif // HAVE_WORKING_SNPRINTF
+
+// Runtime check of ./configure result, throws on error.
+void check_config()
+{
+  check_endianness();
+  check_snprintf();
+}