20 #include <winpr/config.h>
21 #include <winpr/debug.h>
28 #include <winpr/crt.h>
29 #include <winpr/print.h>
34 #define MIN(a, b) (a) < (b) ? (a) : (b)
37 void winpr_HexDump(
const char* tag, UINT32 level,
const void* data,
size_t length)
39 wLog* log = WLog_Get(tag);
40 winpr_HexLogDump(log, level, data, length);
43 void winpr_HexLogDump(wLog* log, UINT32 lvl,
const void* data,
size_t length)
48 const size_t maxlen = 20;
56 const size_t blen = (maxlen + 3ULL) + (WINPR_HEXDUMP_LINE_LENGTH * 3ULL) + 3ULL +
57 WINPR_HEXDUMP_LINE_LENGTH + 1ULL;
62 if (!WLog_IsLevelActive(log, lvl))
68 buffer = malloc(blen);
72 char ebuffer[256] = { 0 };
73 WLog_Print(log, WLOG_ERROR,
"malloc(%" PRIuz
") failed with [%" PRIuz
"] %s", blen, errno,
74 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)));
78 while (offset < length)
80 int rc = _snprintf(&buffer[pos], blen - pos,
"%04" PRIuz
" ", offset);
86 line = length - offset;
88 if (line > WINPR_HEXDUMP_LINE_LENGTH)
89 line = WINPR_HEXDUMP_LINE_LENGTH;
94 rc = _snprintf(&buffer[pos], blen - pos,
"%02" PRIx8
" ", p[i]);
102 for (; i < WINPR_HEXDUMP_LINE_LENGTH; i++)
104 rc = _snprintf(&buffer[pos], blen - pos,
" ");
112 for (
size_t j = 0; j < line; j++)
114 rc = _snprintf(&buffer[pos], blen - pos,
"%c",
115 (p[j] >= 0x20 && p[j] < 0x7F) ? (
char)p[j] :
'.');
123 WLog_Print(log, lvl,
"%s", buffer);
129 WLog_Print(log, lvl,
"[length=%" PRIuz
"] ", length);
134 void winpr_CArrayDump(
const char* tag, UINT32 level,
const void* data,
size_t length,
size_t width)
136 const BYTE* p = data;
138 const size_t llen = ((length > width) ? width : length) * 4ull + 1ull;
140 char* buffer = malloc(llen);
144 char ebuffer[256] = { 0 };
145 WLog_ERR(tag,
"malloc(%" PRIuz
") failed with [%d] %s", llen, errno,
146 winpr_strerror(errno, ebuffer,
sizeof(ebuffer)));
150 while (offset < length)
152 size_t line = length - offset;
159 for (
size_t i = 0; i < line; i++)
161 const int rc = _snprintf(&buffer[pos], llen - pos,
"\\x%02" PRIX8
"", p[i]);
167 WLog_LVL(tag, level,
"%s", buffer);
176 static BYTE value(
char c)
178 if ((c >=
'0') && (c <=
'9'))
180 if ((c >=
'A') && (c <=
'F'))
182 if ((c >=
'a') && (c <=
'f'))
187 size_t winpr_HexStringToBinBuffer(
const char* str,
size_t strLength, BYTE* data,
size_t dataLength)
190 size_t maxStrLen = 0;
191 if (!str || !data || (strLength == 0) || (dataLength == 0))
194 maxStrLen = strnlen(str, strLength);
195 for (
size_t x = 0; x < maxStrLen;)
197 BYTE val = value(str[x++]);
199 val = (BYTE)(val << 4) | (value(str[x++]));
212 size_t winpr_BinToHexStringBuffer(
const BYTE* data,
size_t length,
char* dstStr,
size_t dstSize,
215 const size_t n = space ? 3 : 2;
216 const char bin2hex[] =
"0123456789ABCDEF";
217 const size_t maxLength = MIN(length, dstSize / n);
219 if (!data || !dstStr || (length == 0) || (dstSize == 0))
222 for (
size_t i = 0; i < maxLength; i++)
224 const int ln = data[i] & 0xF;
225 const int hn = (data[i] >> 4) & 0xF;
226 char* dst = &dstStr[i * n];
228 dst[0] = bin2hex[hn];
229 dst[1] = bin2hex[ln];
235 if (space && (maxLength > 0))
237 dstStr[maxLength * n - 1] =
'\0';
238 return maxLength * n - 1;
240 dstStr[maxLength * n] =
'\0';
241 return maxLength * n;
244 char* winpr_BinToHexString(
const BYTE* data,
size_t length, BOOL space)
247 const size_t n = space ? 3 : 2;
248 const size_t size = (length + 1ULL) * n;
249 char* p = (
char*)malloc(size);
254 rc = winpr_BinToHexStringBuffer(data, length, p, size, space);