/* (C) 2003 XDA Developers * Author: Willem Jan Hengeveld * Web: http://www.xda-developers.com/ * * $Header$ */ #include #include "debug.h" // declarations for this file. #include using namespace std; #include "stringutils.h" void vtdebug(TCHAR *msg, va_list ap) { TCHAR *tbuf= new TCHAR[16384]; _vsntprintf(tbuf, 16384, msg, ap); OutputDebugString(tbuf); delete[] tbuf; } void vdebug(char *msg, va_list ap) { char *buf = new char[16384]; _vsnprintf(buf, 16384, msg, ap); TCHAR *tbuf = new TCHAR[16384]; _sntprintf(tbuf, 16384, L"%hs", buf); OutputDebugString(tbuf); delete[] tbuf; delete[] buf; } void tdebug(TCHAR *msg, ...) { va_list ap; va_start(ap, msg); vtdebug(msg, ap); va_end(ap); } void debug(char *msg, ...) { va_list ap; va_start(ap, msg); vdebug(msg, ap); va_end(ap); } void error(char *msg, ...) { DWORD dwErrorCode= GetLastError(); va_list ap; va_start(ap, msg); vdebug(msg, ap); va_end(ap); TCHAR *msgbuf; int rc= FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwErrorCode, 0, (LPTSTR) &msgbuf, 0, NULL); if (rc) debug(" ERROR: %ls\n", msgbuf); else debug(" UNKNOWNERROR: 0x%08lx\n", dwErrorCode); LocalFree(msgbuf); } int g_nDumpUnitSize=1; int g_nMaxWordsPerLine=-1; void hexdumpbytes(string &str, BYTE *buf, int nLength) { str.reserve(str.size()+nLength*3); while(nLength--) { str+= stringformat(" %02x", *buf++); } } void hexdumpwords(string &str, WORD *buf, int nLength) { str.reserve(str.size()+nLength*5); while(nLength--) { str+= stringformat(" %04x", *buf++); } } void hexdumpdwords(string &str, DWORD *buf, int nLength) { str.reserve(str.size()+nLength*9); while(nLength--) { str+= stringformat(" %08x", *buf++); } } void dumpascii(string &str, BYTE *buf, int nLength) { while(nLength--) { BYTE c= *buf++; str += (c>=' ' && c<='~')?c:'.'; } } void writespaces(string &str, int n) { while(n--) { str += ' '; } } void hexdump(DWORD dwOffset, BYTE *buf, int nLength, int nDumpUnitSize, int nMaxUnitsPerLine) { while(nLength) { string line; // is rounding correct here? int nLineLength= min(nLength/nDumpUnitSize, nMaxUnitsPerLine); line.reserve(10+nLineLength*(nDumpUnitSize==1?4:nDumpUnitSize==2?6:nDumpUnitSize==4?10:10)); line += stringformat("%08x", dwOffset); switch(nDumpUnitSize) { case 1: hexdumpbytes(line, buf, nLineLength); break; case 2: hexdumpwords(line, (WORD*)buf, nLineLength); break; case 4: hexdumpdwords(line, (DWORD*)buf, nLineLength); break; } if (nLineLength