From: Oliver Kurth Date: Mon, 20 Aug 2018 19:48:06 +0000 (-0700) Subject: Add Util_Data2Buffer() variant that supports custom separator X-Git-Tag: stable-11.0.0~483 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=63a1cbf60f82022ab37f5003e1b6e49ba9dbb9c8;p=thirdparty%2Fopen-vm-tools.git Add Util_Data2Buffer() variant that supports custom separator \0 means no separator at all. --- diff --git a/open-vm-tools/lib/include/util.h b/open-vm-tools/lib/include/util.h index 9281609c3..9d81f9ec2 100644 --- a/open-vm-tools/lib/include/util.h +++ b/open-vm-tools/lib/include/util.h @@ -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); diff --git a/open-vm-tools/lib/user/util.c b/open-vm-tools/lib/user/util.c index 4f8cb4315..6f56f6947 100644 --- a/open-vm-tools/lib/user/util.c +++ b/open-vm-tools/lib/user/util.c @@ -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; }