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 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
| #include "stdafx.h"//TODO: Faire une version "avec CRT" en bibliothèque.
#include "TestSansCRT.h"
#include "StringBufferC.h"
#include "Inline.h"
/* Helper macros
------------- */
#define ADDRESS_LENGTH (HEX_CHARBUF_SIZE(INT_PTR)-1)
/* Write a char array, minus the terminating null character. */
#define WRITEBUF(bufName) WriteFile(hOut, bufName, sizeof bufName - sizeof *bufName, &nWritten, NULL)
#define WRITECHAR_P(charName) WriteFile(hOut, &(charName), 1, &nWritten, NULL)
#define WRITECHAR_S(charStr) WriteFile(hOut, charStr, 1, &nWritten, NULL)
struct dumpParams
{
unsigned int nBytesPerLine;
unsigned int nSpaces1;
unsigned int nSpaces2;
unsigned char const * pcByMem;
unsigned char const * pcStart;
unsigned char const * pcEnd; //pcEnd is the address IMMEDIATELY AFTER the last valid one.
};
/* 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] */
BOOL bAlign /*[in] */
)
{
MY_ASSERT(pParams != NULL);
//Calculate number of bytes per line
{
int nSpaces = 0;
unsigned int nBytesPerLine = 0;
unsigned int const nColsBytes = nCols-1-ADDRESS_LENGTH-1; /*End-of-line, address-in-hex, space.*/
if(nCols <= 1+ADDRESS_LENGTH+1 || nColsBytes < 4)
return FALSE;
nBytesPerLine = LowerPowerOfTwo(nColsBytes/4);
MY_ASSERT(nBytesPerLine > 0); //shall be OK, since tested before.
//Additional spaces
nSpaces = (nColsBytes-(nBytesPerLine*4));
MY_ASSERT(nSpaces >= 0);
pParams->nSpaces2 = nSpaces/2;
pParams->nSpaces1 = nSpaces - pParams->nSpaces2;
pParams->nBytesPerLine = nBytesPerLine;
}
MY_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.
if(bAlign)
{
pcStart -= (INT_PTR)pcStart%nBytesPerLine;
MY_ASSERT((INT_PTR)pcStart%nBytesPerLine == 0);
}
//Always align pcEnd with pcStart
{
size_t unalEnd = (pcEnd-pcStart)%nBytesPerLine;
if(unalEnd!=0)
{
pcEnd += (nBytesPerLine-unalEnd);
}
MY_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 CCPP_INLINE void WriteChars(HANDLE hOut, char c, unsigned int n)
{
DWORD nWritten;
unsigned int i;
for(i=0 ; i<n ; i++)
WRITECHAR_P(c);
}
//-----------------------------------------------------------------------------
/* Memory dump function
-------------------- */
EXTERN_C void DumpMemory(
HANDLE hOut, /*[in/modif] Output handle. */
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. */
BOOL bAlign /*[in] If true, lines start at addresses multiple of their size.*/
)
{
//static const struct dumpParams zeroDumpParams = {0};
struct dumpParams params;// = zeroDumpParams;
//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, bAlign))
{
char msg[] = "Error\n";
DWORD nWritten;
WRITEBUF(msg);
return;
}
//Dump the memory according to the processed parameters.
{
unsigned char const * pcCurrent = NULL;
size_t iByteLine = 0;
BUF_DECL(caracs, char, 40);
BOOL bBufIsValid = FALSE;
//The NUL terminator is not necessary here for WriteFile(), but it may be for other functions.
BUF_START(caracs, params.nBytesPerLine+1);
bBufIsValid = (BUF_GETCOUNT(caracs) >= params.nBytesPerLine+1);
if(bBufIsValid)
BUF_GET(caracs)[params.nBytesPerLine] = '\0';
for(pcCurrent = params.pcStart ; pcCurrent<params.pcEnd ; pcCurrent++)
{
DWORD nWritten;
//If at start of line, display address
if(iByteLine==0)
{
char bufAddr[HEX_CHARBUF_SIZE(INT_PTR)];
FormatHexadecimalFullA((ULONG_PTR)pcCurrent, bufAddr, ARRAYSIZE(bufAddr));
WRITEBUF(bufAddr);
//Write spaces
WriteChars(hOut, ' ', 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(hOut, ' ', 2);
//Add a space to the buffer instead of the character.
if(bBufIsValid)
BUF_GET(caracs)[iByteLine] = ' ';
}
else
{
//Write the byte
unsigned char const by = *pcCurrent;
char bufByte[HEX_CHARBUF_SIZE(unsigned char)];
FormatHexadecimalFullA(by, bufByte, ARRAYSIZE(bufByte));
WRITEBUF(bufByte);
//Add the character to the buffer
if(bBufIsValid)
BUF_GET(caracs)[iByteLine] = (by < 32 ? '.' : by);
}
//Increment iByteLine now, allowing correct position of the "minus" char.
iByteLine++;
//Write the space between bytes.
{
if(iByteLine==params.nBytesPerLine/2 && iByteLine!=0)
WRITECHAR_S("-");
else
WRITECHAR_S(" ");
}
//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(hOut, ' ', params.nSpaces2);
//Write the caracs
if(bBufIsValid)
WriteFile(hOut, BUF_GET(caracs), params.nBytesPerLine, &nWritten, NULL);
else
WriteChars(hOut, '-', params.nBytesPerLine);
//Next line.
iByteLine = 0;
WRITECHAR_S("\n");
}
}//for
//Debug: The NUL terminator must not have been overwritten
if(bBufIsValid)
MY_ASSERT( BUF_GET(caracs)[params.nBytesPerLine] == '\0' );
BUF_END(caracs);
}//local variables
}
/* Test helper macro */
#define TESTDUMP(bufName, align) DumpMemory(hOut, 80, bufName, ARRAYSIZE(bufName), align)
/* Test function 1
--------------- */
void TestDump1(void)
{
char buf1[] = {0x11, 0x22, 0x33, 0x44, 0x55};
char buf2[] = {0x66, 0x77, 0x88, 0x99, 0xAA};
char buf3[] = {0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
char msg1[] = "Aligned:\n";
char msg2[] = "Unaligned:\n";
char msg3[] = "----\n";
char msg4[] = "\n";
DWORD nWritten;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
WRITEBUF(msg1);
TESTDUMP(buf1, TRUE);
WRITEBUF(msg3);
TESTDUMP(buf2, TRUE);
WRITEBUF(msg3);
TESTDUMP(buf3, TRUE);
WRITEBUF(msg4);
WRITEBUF(msg2);
TESTDUMP(buf1, FALSE);
WRITEBUF(msg3);
TESTDUMP(buf2, FALSE);
WRITEBUF(msg3);
TESTDUMP(buf3, FALSE);
}
/* Test helper macro */
#define TESTDUMP2(bufName, size, align) DumpMemory(hOut, 80, bufName, size, align)
/* Test function 2
--------------- */
void TestDump2(void)
{
char buf4[] = {0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
char *ptr1 = buf4;
char *ptr2 = buf4+5;
char *ptr3 = buf4+10;
char buf5[] = "Ah que Coucou, C'est moi Frédéric! Je suis content, c'est moi qui ai programmé ça, nanana!";
char msg1[] = "Aligned:\n";
char msg2[] = "Unaligned:\n";
char msg3[] = "----\n";
char msg4[] = "\n";
DWORD nWritten;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
WRITEBUF(msg1);
TESTDUMP2(ptr1, 5, TRUE);
WRITEBUF(msg3);
TESTDUMP2(ptr2, 5, TRUE);
WRITEBUF(msg3);
TESTDUMP2(ptr3, 5, TRUE);
WRITEBUF(msg4);
WRITEBUF(msg2);
TESTDUMP2(ptr1, 5, FALSE);
WRITEBUF(msg3);
TESTDUMP2(ptr2, 5, FALSE);
WRITEBUF(msg3);
TESTDUMP2(ptr3, 5, FALSE);
WRITEBUF(msg4);
WRITEBUF(msg1);
TESTDUMP2(buf5+7, ARRAYSIZE(buf5)-7, TRUE);
WRITEBUF(msg2);
TESTDUMP2(buf5+7, ARRAYSIZE(buf5)-7, FALSE);
} |
Partager