]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Add Util_Data2Buffer() variant that supports custom separator
authorOliver Kurth <okurth@vmware.com>
Mon, 20 Aug 2018 19:48:06 +0000 (12:48 -0700)
committerOliver Kurth <okurth@vmware.com>
Mon, 20 Aug 2018 19:48:06 +0000 (12:48 -0700)
\0 means no separator at all.

open-vm-tools/lib/include/util.h
open-vm-tools/lib/user/util.c

index 9281609c330edf85962a8e2fa66fe509d6dfda77..9d81f9ec2f70c3a19cdd86179f100bfccfd59bfa 100644 (file)
@@ -67,8 +67,12 @@ int Util_HasAdminPriv(void);
 #if defined _WIN32 && defined USERLEVEL
 int Util_TokenHasAdminPriv(HANDLE token);
 #endif
+
 Bool Util_Data2Buffer(char *buf, size_t bufSize, const void *data0,
                       size_t dataSize);
+Bool Util_Data2BufferEx(char *buf, size_t bufSize, const void *data0,
+                        size_t dataSize, char sep);
+
 char *Util_GetCanonicalPath(const char *path);
 #ifdef _WIN32
 char *Util_CompatGetCanonicalPath(const char *path);
index 4f8cb4315e9b85ce7ea16d237f31858b6ab1905d..6f56f6947f0f19d74d6e593b882d05538851e428 100644 (file)
@@ -376,13 +376,14 @@ Util_Throttle(uint32 count)  // IN:
  *
  * Util_Data2Buffer --
  *
- *     Format binary data for printing
+ *    Format binary data for printing.
+ *    Uses space as byte separator.
  *
  * Results:
- *     TRUE if all data fits into buffer, FALSE otherwise.
+ *    TRUE if all data fits into buffer, FALSE otherwise.
  *
  * Side effects:
- *     None
+ *    None
  *
  *----------------------------------------------------------------------
  */
@@ -393,15 +394,46 @@ Util_Data2Buffer(char *buf,         // OUT
                  const void *data0, // IN
                  size_t dataSize)   // IN
 {
-   size_t n;
+   return Util_Data2BufferEx(buf, bufSize, data0, dataSize, ' ');
+}
+
+
+/*
+ *----------------------------------------------------------------------
+ *
+ * Util_Data2BufferEx --
+ *
+ *    Format binary data for printing.
+ *    Uses custom byte separator. None if set to '\0'.
+ *
+ * Results:
+ *    TRUE if all data fits into buffer, FALSE otherwise.
+ *
+ * Side effects:
+ *    None
+ *
+ *----------------------------------------------------------------------
+ */
+
+Bool
+Util_Data2BufferEx(char *buf,         // OUT
+                   size_t bufSize,    // IN
+                   const void *data0, // IN
+                   size_t dataSize,   // IN
+                   char sep)          // IN
+{
+   size_t n; /* Chars to process from data0. */
+   const Bool useSeparator = sep != 0;
+
+   /* Number of processed chars that will fit in buf and leave space for trailing NUL. */
+   const size_t outChars = useSeparator ? bufSize / 3 : (bufSize - 1) / 2;
 
    /* At least 1 byte (for NUL) must be available. */
    if (!bufSize) {
       return FALSE;
    }
 
-   bufSize = bufSize / 3;
-   n = MIN(dataSize, bufSize);
+   n = MIN(dataSize, outChars);
    if (n != 0) {
       const uint8 *data = data0;
 
@@ -410,14 +442,18 @@ Util_Data2Buffer(char *buf,         // OUT
 
          *buf++ = digits[*data >> 4];
          *buf++ = digits[*data & 0xF];
-         *buf++ = ' ';
+         if (useSeparator) {
+            *buf++ = sep;
+         }
          data++;
          n--;
       }
-      buf--;
+      if (useSeparator) {
+         buf--; /* Overwrite the last separator with NUL. */
+      }
    }
    *buf = 0;
-   return dataSize <= bufSize;
+   return dataSize <= outChars;
 }