1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
| /*
HexAscii Dump.c:
Implements an hexadecimal+ASCII dump to console.
*/
#include "HexAscii Dump.h"
#include <stdlib.h>
#include <assert.h>
/* Helper macros
------------- */
#ifndef ARRAYSIZE
#define ARRAYSIZE(x) (sizeof x / sizeof x[0])
#endif
typedef enum BOOL { FALSE, TRUE} BOOL;
struct dumpParams
{
unsigned int nBytesPerLine;
unsigned int nSpaces1;
unsigned int nSpaces2;
unsigned int realAddressLength;
BOOL displayAddress;
unsigned char const * pcByMem;
unsigned char const * pcStart;
unsigned char const * pcEnd; /* pcEnd is the address IMMEDIATELY AFTER the last valid one, rounded up on line length. */
};
/* Helper function for number of bytes to display
---------------------------------------------- */
static unsigned int LowerPowerOfTwo(unsigned int in)
{
/*Search the first 'set' bit, from msb to lsb.*/
unsigned int bit = 0x80000000u;
if(in==0)
return 0;
while((in & bit)==0)
{
/*Bit is too high: shift it and retry.*/
bit >>= 1;
}
return bit;
}
/* Helper function for calculating the dump parameters
--------------------------------------------------- */
static BOOL ProcessParams(
struct dumpParams *pParams, /*[out] */
unsigned int nCols, /*[in] */
unsigned char const * pcByMem, /*[in] pcMem, implicitely cast as a char pointer. */
size_t cbMem, /*[in] */
DumpAddressMode addressMode /*[in] */
)
{
assert(pParams != NULL);
/*Calculate real address length*/
if(addressMode != DumpNoAddress)
{
void* p=NULL;
#ifdef _MSC_VER
int length = _scprintf("%p", p);
#else
int length = snprintf(NULL, "%p", p);
#endif
if(length < 0)
return FALSE;
pParams->realAddressLength = length;
pParams->displayAddress = TRUE;
}
else
{
pParams->realAddressLength = 0;
pParams->displayAddress = FALSE;
}
/*Calculate number of bytes per line, and spacing*/
{
int nSpaces = 0;
unsigned int nBytesPerLine = 0;
unsigned int spacedAddressLength = pParams->realAddressLength + (pParams->displayAddress ? 1 : 0);
unsigned int const nColsBytes = nCols - spacedAddressLength - 1; /*subtract Address-in-hex (with space), End-of-line, .*/
/*If not enough for four bytes, fail.*/
if(nCols <= spacedAddressLength+1 || nColsBytes < 4)
return FALSE;
nBytesPerLine = LowerPowerOfTwo(nColsBytes/4);
assert(nBytesPerLine > 0); /*shall be OK, since tested before.*/
/*Additional spacing*/
nSpaces = (nColsBytes-(nBytesPerLine*4));
assert(nSpaces >= 0);
if(pParams->displayAddress)
pParams->nSpaces2 = nSpaces/2;
else
pParams->nSpaces2 = nSpaces;
pParams->nSpaces1 = nSpaces - pParams->nSpaces2;
pParams->nBytesPerLine = nBytesPerLine;
}
assert(pParams->nBytesPerLine > 0);
/*Start&End pointers, with alignment.*/
{
unsigned int const nBytesPerLine = pParams->nBytesPerLine;
unsigned char const * pcStart = pcByMem;
unsigned char const * pcEnd = pcByMem+cbMem; /*pcEnd is the address IMMEDIATELY AFTER the last valid one here.*/
/*If aligning, round pcStart down.*/
if(addressMode == DumpAddressAligned)
{
pcStart -= (intptr_t)pcStart%nBytesPerLine;
assert((intptr_t)pcStart%nBytesPerLine == 0);
}
/*Always align pcEnd with pcStart*/
{
size_t unalEnd = (pcEnd-pcStart)%nBytesPerLine;
if(unalEnd!=0)
{
pcEnd += (nBytesPerLine-unalEnd);
}
assert((pcStart-pcEnd)%nBytesPerLine == 0);
}
/*Set pointers in the parameter structure.*/
pParams->pcByMem = pcByMem;
pParams->pcStart = pcStart;
pParams->pcEnd = pcEnd;
}
return TRUE;
}
/* Helper function for writing a string of identical chars
------------------------------------------------------- */
static void WriteChars(FILE* pfOut, char c, unsigned int n)
{
unsigned int i;
for(i=0 ; i<n ; i++)
putc(c, pfOut);
}
/*Function for hiding bytes that can't be printed for some reason or other*/
static BOOL IsPrintableByte(unsigned char by)
{
return by!=127 //DEL
&& by >= 32 //C0 control characters
#ifdef _WIN32
&& by!=0x81 && by!=0x8D && by!=0x8F //Five characters in the C1 block still non-printable in Windows-1252
&& by!=0x90 && by!=0x9D //Five characters in the C1 block still non-printable in Windows-1252
&& by!=0x95 //Bullet, non-printable when dumped on Console
#else
&& !(by >= 0x80 && by <= 0x9F) //Assume all C1 characters are non-printable on non-Windows OSes
#endif
// && by!=0x98 //Tilde, because textbox treats it as combining.
// && by!=0xAD; //SHY (Soft Hyphen) character, which disappears on textbox.
;
}
/*---------------------------------------------------------------------------*/
/* Memory dump function
-------------------- */
EXTERN_C void DumpMemory(
FILE* pfOut, /*[in/modif] Output stream. */
unsigned int nCols, /*[in] Number of columns of display. Must be at least 14 on 32-bit machines. */
void const * pcMem, /*[in] Memory block to dump. */
size_t cbMem, /*[in] Size to dump, in bytes. */
DumpAddressMode addressMode /*[in] Whether to display the address, and if so, whether to align on line size.*/
)
{
struct dumpParams params = {0};
/*Analyze the parameters to set variables such as the real start and end of the dump,
the number of bytes in one line, etc.*/
if(!ProcessParams(¶ms, nCols, pcMem, cbMem, addressMode))
{
fputs("Error processing parameters.\n", stderr);
return;
}
/*Dump the memory according to the processed parameters.*/
{
unsigned char const * pcCurrent = NULL;
size_t iByteLine = 0;
/*Buffer for individual characters, written at the end of each line*/
char stackBuf[40]="", *heapBuf=NULL, *buf=NULL;
size_t bufSize = params.nBytesPerLine+1;
buf = bufSize > ARRAYSIZE(buf) ? (heapBuf=malloc(bufSize)) : stackBuf;
if(buf != NULL)
buf[params.nBytesPerLine] = '\0';
for(pcCurrent = params.pcStart ; pcCurrent<params.pcEnd ; pcCurrent++)
{
/*If at start of line, display address and the spaces that follow it*/
if(iByteLine==0 && params.displayAddress)
{
fprintf(pfOut, "%p", (void const*)pcCurrent);
/*Write spaces*/
WriteChars(pfOut, ' ', params.nSpaces1+1);
}
/*If before the start or after the end, display spaces instead of the data.
If in the right zone, display the byte data (The pointer is dereferenced only here).*/
if(pcCurrent < params.pcByMem || pcCurrent >= params.pcByMem+cbMem)
{
/*Write two spaces instead of the byte.*/
WriteChars(pfOut, ' ', 2);
/*Add a space to the buffer instead of the character.*/
if(buf != NULL)
buf[iByteLine] = ' ';
}
else
{
/*Write the byte*/
unsigned char const by = *pcCurrent;
fprintf(pfOut, "%02X", by);
/*Add the character to the buffer*/
if(buf != NULL)
buf[iByteLine] = (IsPrintableByte(by) ? by : '.');
}
/*Increment iByteLine now, allowing correct position of the "minus" char.*/
iByteLine++;
/*Write the space between bytes.*/
{
if(iByteLine==params.nBytesPerLine/2 && iByteLine!=0)
putc('-', pfOut);
else
putc(' ', pfOut);
}
/*If at end of line, display the character representation of the bytes.*/
if(iByteLine ==params.nBytesPerLine)
{
/*Write spaces (no +1 here, because written above)*/
WriteChars(pfOut, ' ', params.nSpaces2);
/*Write the caracs*/
if(buf != NULL)
fprintf(pfOut, "%s", buf);
else
WriteChars(pfOut, '-', params.nBytesPerLine);
/*Next line.*/
iByteLine = 0;
putc('\n', pfOut);
}
}/*for*/
/*Debug: The NUL terminator must not have been overwritten*/
if(buf != NULL)
assert( buf[params.nBytesPerLine] == '\0' );
buf=NULL;
free(heapBuf), heapBuf=NULL;
}/*local variables*/
} |
Partager