From 321a197209a3cfcf9821dc0de62a8426f8020f63 Mon Sep 17 00:00:00 2001 From: Jeremy White Date: Fri, 12 Nov 1999 01:38:12 +0000 Subject: [PATCH] Add debugstr_hex_dump to allow display hex dumps of data in any debugging message. --- include/debugstr.h | 2 + misc/debugstr.c | 106 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) diff --git a/include/debugstr.h b/include/debugstr.h index fd7124068c..4c5323352d 100644 --- a/include/debugstr.h +++ b/include/debugstr.h @@ -14,6 +14,7 @@ extern LPSTR debugstr_w (LPCWSTR s); extern LPSTR debugres_a (LPCSTR res); extern LPSTR debugres_w (LPCWSTR res); extern void debug_dumpstr (LPCSTR s); +extern LPSTR debugstr_hex_dump (const void *ptr, int len); #ifdef __GNUC__ extern int dbg_printf(const char *format, ...) __attribute__((format (printf,1,2))); @@ -21,4 +22,5 @@ extern int dbg_printf(const char *format, ...) __attribute__((format (printf,1,2 extern int dbg_printf(const char *format, ...); #endif + #endif /* __WINE_DEBUGSTR_H */ diff --git a/misc/debugstr.c b/misc/debugstr.c index 73a4a712be..6ffdf31fa1 100644 --- a/misc/debugstr.c +++ b/misc/debugstr.c @@ -1,6 +1,8 @@ #include #include #include +#include +#include #include "debugstr.h" #include "debugtools.h" @@ -198,3 +200,107 @@ int dbg_printf(const char *format, ...) return ret; } + + +/*--< Function >--------------------------------------------------------- +** +** debugstr_hex_dump +** +** Description: +** This function creates a hex dump, with a readable ascii +** section, for displaying memory. +** +** Parameters: +** 1. ptr Pointer to memory +** 2. len How much to dump. +** +** Returns: +** Temporarily allocated buffer, with the hex dump in it. +** Don't rely on this pointer being around for very long, just +** long enough to use it in a TRACE statement; e.g.: +** TRACE("struct dump is \n%s", debugstr_hex_dump(&x, sizeof(x))); +** +**-------------------------------------------------------------------------*/ +LPSTR +debugstr_hex_dump (const void *ptr, int len) +{ + /* Locals */ + char dumpbuf[59]; + char charbuf[20]; + char tempbuf[8]; + const char *p; + int i; + unsigned int nosign; + LPSTR dst; + LPSTR outptr; + +/* Begin function dbg_hex_dump */ + + /*----------------------------------------------------------------------- + ** Allocate an output buffer + ** A reasonable value is one line overhand (80 chars), and + ** then one line (80) for every 16 bytes. + **---------------------------------------------------------------------*/ + outptr = dst = gimme1 ((len * (80 / 16)) + 80); + + /*----------------------------------------------------------------------- + ** Loop throught the input buffer, one character at a time + **---------------------------------------------------------------------*/ + for (i = 0, p = ptr; (i < len); i++, p++) + { + + /*------------------------------------------------------------------- + ** If we're just starting a line, + ** we need to possibly flush the old line, and then + ** intialize the line buffer. + **-----------------------------------------------------------------*/ + if ((i % 16) == 0) + { + if (i) + { + sprintf(outptr, " %-43.43s %-16.16s\n", dumpbuf, charbuf); + outptr += strlen(outptr); + } + sprintf (dumpbuf, "%04x: ", i); + strcpy (charbuf, ""); + } + + /*------------------------------------------------------------------- + ** Add the current data byte to the dump section. + **-----------------------------------------------------------------*/ + nosign = (unsigned char) *p; + sprintf (tempbuf, "%02X", nosign); + + /*------------------------------------------------------------------- + ** If we're two DWORDS through, add a hyphen for readability, + ** if it's a DWORD boundary, add a space for more + ** readability. + **-----------------------------------------------------------------*/ + if ((i % 16) == 7) + strcat(tempbuf, " - "); + else if ( (i % 4) == 3) + strcat(tempbuf, " "); + strcat (dumpbuf, tempbuf); + + /*------------------------------------------------------------------- + ** Add the current byte to the character display part of the + ** hex dump + **-----------------------------------------------------------------*/ + sprintf (tempbuf, "%c", isprint(*p) ? *p : '.'); + strcat (charbuf, tempbuf); + } + + /*----------------------------------------------------------------------- + ** Flush the last line, if any + **---------------------------------------------------------------------*/ + if (i > 0) + { + sprintf(outptr, " %-43.43s %-16.16s\n", dumpbuf, charbuf); + outptr += strlen(outptr); + } + + return(dst); +} /* End function dbg_hex_dump */ + + +