Shorten stats from EncodeManager using SI/IEC prefixes

Also avoids %lld which isn't supported on Windows.
diff --git a/common/rfb/EncodeManager.cxx b/common/rfb/EncodeManager.cxx
index 954f29b..9122cd7 100644
--- a/common/rfb/EncodeManager.cxx
+++ b/common/rfb/EncodeManager.cxx
@@ -163,6 +163,8 @@
 
   double ratio;
 
+  char a[1024], b[1024];
+
   rects = 0;
   pixels = bytes = equivalent = 0;
 
@@ -190,19 +192,23 @@
 
       ratio = (double)stats[i][j].equivalent / stats[i][j].bytes;
 
-      vlog.info("    %s: %u rects, %llu pixels",
-                encoderTypeName((EncoderType)j),
-                stats[i][j].rects, stats[i][j].pixels);
-      vlog.info("    %*s  %llu bytes (%g ratio)",
-                strlen(encoderTypeName((EncoderType)j)), "",
-                stats[i][j].bytes, ratio);
+      siPrefix(stats[i][j].rects, "rects", a, sizeof(a));
+      siPrefix(stats[i][j].pixels, "pixels", b, sizeof(b));
+      vlog.info("    %s: %s, %s", encoderTypeName((EncoderType)j), a, b);
+      iecPrefix(stats[i][j].bytes, "B", a, sizeof(a));
+      vlog.info("    %*s  %s (1:%g ratio)",
+                (int)strlen(encoderTypeName((EncoderType)j)), "",
+                a, ratio);
     }
   }
 
   ratio = (double)equivalent / bytes;
 
-  vlog.info("  Total: %u rects, %llu pixels", rects, pixels);
-  vlog.info("         %llu bytes (%g ratio)", bytes, ratio);
+  siPrefix(rects, "rects", a, sizeof(a));
+  siPrefix(pixels, "pixels", b, sizeof(b));
+  vlog.info("  Total: %s, %s", a, b);
+  iecPrefix(bytes, "B", a, sizeof(a));
+  vlog.info("         %s (1:%g ratio)", a, ratio);
 }
 
 bool EncodeManager::supported(int encoding)
diff --git a/common/rfb/util.cxx b/common/rfb/util.cxx
index a41ad96..2b3c9f4 100644
--- a/common/rfb/util.cxx
+++ b/common/rfb/util.cxx
@@ -34,6 +34,7 @@
 #include <config.h>
 #endif
 
+#include <stdio.h>
 #include <sys/time.h>
 
 #include <rfb/util.h>
@@ -110,4 +111,44 @@
 
     return diff;
   }
+
+  static size_t doPrefix(long long value, const char *unit,
+                         char *buffer, size_t maxlen,
+                         unsigned divisor, const char **prefixes,
+                         size_t prefixCount) {
+    double newValue;
+    size_t prefix, len;
+
+    newValue = value;
+    prefix = 0;
+    while (newValue >= divisor) {
+      if (prefix >= prefixCount)
+        break;
+      newValue /= divisor;
+      prefix++;
+    }
+
+    len = snprintf(buffer, maxlen, "%g %s%s", newValue,
+                   (prefix == 0) ? "" : prefixes[prefix-1], unit);
+    buffer[maxlen-1] = '\0';
+
+    return len;
+  }
+
+  static const char *siPrefixes[] =
+    { "k", "M", "G", "T", "P", "E", "Z", "Y" };
+  static const char *iecPrefixes[] =
+    { "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi" };
+
+  size_t siPrefix(long long value, const char *unit,
+                  char *buffer, size_t maxlen) {
+    return doPrefix(value, unit, buffer, maxlen, 1000, siPrefixes,
+                    sizeof(siPrefixes)/sizeof(*siPrefixes));
+  }
+
+  size_t iecPrefix(long long value, const char *unit,
+                   char *buffer, size_t maxlen) {
+    return doPrefix(value, unit, buffer, maxlen, 1024, iecPrefixes,
+                    sizeof(iecPrefixes)/sizeof(*iecPrefixes));
+  }
 };
diff --git a/common/rfb/util.h b/common/rfb/util.h
index 13dbe68..98ce642 100644
--- a/common/rfb/util.h
+++ b/common/rfb/util.h
@@ -90,6 +90,11 @@
 
   // Returns time elapsed since given moment in milliseconds.
   unsigned msSince(const struct timeval *then);
+
+  size_t siPrefix(long long value, const char *unit,
+                  char *buffer, size_t maxlen);
+  size_t iecPrefix(long long value, const char *unit,
+                   char *buffer, size_t maxlen);
 }
 
 // Some platforms (e.g. Windows) include max() and min() macros in their