home *** CD-ROM | disk | FTP | other *** search
- #include <ctype.h>
- #include "DebugUtils.h"
-
-
-
- void printmem(const void *data,size_t len)
- {
- fprintmem(stdout,data,len);
- }
-
-
-
- void fprintmem(FILE *stream,const void *data,size_t len)
- {
- static char sBin2Hex[] = "0123456789ABCDEF";
- static char sPrintTable[256];
- static Boolean sNeedInit = true;
- char ascii[17],hex[41],*hstr,*astr;
- Boolean partial = (len < 16);
- UInt32 spaces,index;
- UInt8 byte;
-
- if (sNeedInit)
- {
- for (index = 0;index < 256;index++)
- sPrintTable[index] = (index & 0x80) ? '.' : isprint(index) ? index : '.';
-
- sNeedInit = false;
- }
-
- // Print header line.
- fprintf(stream,"Displaying %lu bytes from %08lX\n",len,(UInt32)data);
-
- // Print full lines.
- for (ascii[16] = '\0';len >= 16;len -= 16,(UInt8*)data += 16)
- {
- for (index = 0;index < 16;index++)
- ascii[index] = sPrintTable[*((UInt8*)data + index)];
-
- fprintf(stream,"%08lX %04X %04X %04X %04X %04X %04X %04X %04X '%s'\n",(UInt32)data,*((UInt16*)data + 0),
- *((UInt16*)data + 1),*((UInt16*)data + 2),*((UInt16*)data + 3),*((UInt16*)data + 4),
- *((UInt16*)data + 5),*((UInt16*)data + 6),*((UInt16*)data + 7),ascii);
- }
-
- // Print remainder/partial line.
- if (len > 0)
- {
- spaces = 0;
- hstr = &hex[0];
- astr = &ascii[0];
- for (index = 0;index < len;index++)
- {
- spaces = 0;
- byte = *((UInt8*)data + index);
- *astr++ = sPrintTable[byte];
- *hstr++ = sBin2Hex[byte >> 4];
- *hstr++ = sBin2Hex[byte & 15];
-
- if ((index & 7) == 7)
- {
- *hstr++ = ' ';
- spaces += 1;
- }
-
- if ((index & 1) == 1)
- {
- *hstr++ = ' ';
- spaces += 1;
- }
- }
-
- while(spaces < 2)
- {
- *hstr++ = ' ';
- spaces += 1;
- }
-
- *hstr = '\0';
- *astr = '\0';
-
- fprintf(stream,"%08lX %s%*s'%s'\n",(UInt32)data,hex,(int)(partial ? 0 : (42 - strlen(hex))),"",ascii);
- }
- }
-