📄 otherfunctions.cpp
字号:
{
if (hash == NULL)
return CString();
CString strInfo(_T("File="));
LPCTSTR pszName = DbgGetFileNameFromID(hash);
if (pszName != NULL)
strInfo += pszName;
else
strInfo += md4str(hash);
return strInfo;
}
CString DbgGetBlockInfo(const Requested_Block_Struct* block)
{
return DbgGetBlockInfo(block->StartOffset, block->EndOffset);
}
CString DbgGetBlockInfo(uint32 StartOffset, uint32 EndOffset)
{
CString strInfo;
strInfo.Format(_T("%u-%u (%u bytes)"), StartOffset, EndOffset, EndOffset - StartOffset + 1);
strInfo.AppendFormat(_T(", Part %u"), StartOffset/PARTSIZE);
if (StartOffset/PARTSIZE != EndOffset/PARTSIZE)
strInfo.AppendFormat(_T("-%u(**)"), EndOffset/PARTSIZE);
strInfo.AppendFormat(_T(", Block %u"), StartOffset/EMBLOCKSIZE);
if (StartOffset/EMBLOCKSIZE != EndOffset/EMBLOCKSIZE)
{
strInfo.AppendFormat(_T("-%u"), EndOffset/EMBLOCKSIZE);
if (EndOffset/EMBLOCKSIZE - StartOffset/EMBLOCKSIZE > 1)
strInfo += _T("(**)");
}
return strInfo;
}
CString DbgGetBlockFileInfo(const Requested_Block_Struct* block, const CPartFile* partfile)
{
CString strInfo(DbgGetBlockInfo(block));
strInfo += _T("; ");
strInfo += DbgGetFileInfo(partfile ? partfile->GetFileHash() : NULL);
return strInfo;
}
int GetHashType(const uchar* hash)
{
if (hash[5] == 13 && hash[14] == 110)
return SO_OLDEMULE;
else if (hash[5] == 14 && hash[14] == 111)
return SO_EMULE;
else if (hash[5] == 'M' && hash[14] == 'L')
return SO_MLDONKEY;
else
return SO_UNKNOWN;
}
LPCTSTR DbgGetHashTypeString(const uchar* hash)
{
int iHashType = GetHashType(hash);
if (iHashType == SO_EMULE)
return _T("eMule");
if (iHashType == SO_MLDONKEY)
return _T("MLdonkey");
if (iHashType == SO_OLDEMULE)
return _T("Old eMule");
ASSERT( iHashType == SO_UNKNOWN );
return _T("Unknown");
}
CString DbgGetClientID(uint32 nClientID)
{
CString strClientID;
if (IsLowID(nClientID))
strClientID.Format(_T("LowID=%u"), nClientID);
else
strClientID = ipstr(nClientID);
return strClientID;
}
#define _STRVAL(o) {_T(#o), o}
CString DbgGetDonkeyClientTCPOpcode(UINT opcode)
{
static const struct
{
LPCTSTR pszOpcode;
UINT uOpcode;
} _aOpcodes[] =
{
_STRVAL(OP_HELLO),
_STRVAL(OP_SENDINGPART),
_STRVAL(OP_REQUESTPARTS),
_STRVAL(OP_FILEREQANSNOFIL),
_STRVAL(OP_END_OF_DOWNLOAD),
_STRVAL(OP_ASKSHAREDFILES),
_STRVAL(OP_ASKSHAREDFILESANSWER),
_STRVAL(OP_HELLOANSWER),
_STRVAL(OP_CHANGE_CLIENT_ID),
_STRVAL(OP_MESSAGE),
_STRVAL(OP_SETREQFILEID),
_STRVAL(OP_FILESTATUS),
_STRVAL(OP_HASHSETREQUEST),
_STRVAL(OP_HASHSETANSWER),
_STRVAL(OP_STARTUPLOADREQ),
_STRVAL(OP_ACCEPTUPLOADREQ),
_STRVAL(OP_CANCELTRANSFER),
_STRVAL(OP_OUTOFPARTREQS),
_STRVAL(OP_REQUESTFILENAME),
_STRVAL(OP_REQFILENAMEANSWER),
_STRVAL(OP_CHANGE_SLOT),
_STRVAL(OP_QUEUERANK),
_STRVAL(OP_ASKSHAREDDIRS),
_STRVAL(OP_ASKSHAREDFILESDIR),
_STRVAL(OP_ASKSHAREDDIRSANS),
_STRVAL(OP_ASKSHAREDFILESDIRANS),
_STRVAL(OP_ASKSHAREDDENIEDANS)
};
for (int i = 0; i < ARRSIZE(_aOpcodes); i++)
{
if (_aOpcodes[i].uOpcode == opcode)
return _aOpcodes[i].pszOpcode;
}
CString strOpcode;
strOpcode.Format(_T("0x%02x"), opcode);
return strOpcode;
}
CString DbgGetMuleClientTCPOpcode(UINT opcode)
{
static const struct
{
LPCTSTR pszOpcode;
UINT uOpcode;
} _aOpcodes[] =
{
_STRVAL(OP_EMULEINFO),
_STRVAL(OP_EMULEINFOANSWER),
_STRVAL(OP_COMPRESSEDPART),
_STRVAL(OP_QUEUERANKING),
_STRVAL(OP_FILEDESC),
_STRVAL(OP_REQUESTSOURCES),
_STRVAL(OP_ANSWERSOURCES),
_STRVAL(OP_PUBLICKEY),
_STRVAL(OP_SIGNATURE),
_STRVAL(OP_SECIDENTSTATE),
_STRVAL(OP_REQUESTPREVIEW),
_STRVAL(OP_PREVIEWANSWER),
_STRVAL(OP_MULTIPACKET),
_STRVAL(OP_MULTIPACKETANSWER),
_STRVAL(OP_PEERCACHE_QUERY),
_STRVAL(OP_PEERCACHE_ANSWER),
_STRVAL(OP_PEERCACHE_ACK),
_STRVAL(OP_PUBLICIP_ANSWER),
_STRVAL(OP_PUBLICIP_REQ)
};
for (int i = 0; i < ARRSIZE(_aOpcodes); i++)
{
if (_aOpcodes[i].uOpcode == opcode)
return _aOpcodes[i].pszOpcode;
}
CString strOpcode;
strOpcode.Format(_T("0x%02x"), opcode);
return strOpcode;
}
#undef _STRVAL
CString DbgGetClientTCPPacket(UINT protocol, UINT opcode, UINT size)
{
CString str;
if (protocol == OP_EDONKEYPROT)
str.Format(_T("protocol=eDonkey opcode=%s size=%u"), DbgGetDonkeyClientTCPOpcode(opcode), size);
else if (protocol == OP_PACKEDPROT)
str.Format(_T("protocol=Packed opcode=%s size=%u"), DbgGetMuleClientTCPOpcode(opcode), size);
else if (protocol == OP_EMULEPROT)
str.Format(_T("protocol=eMule opcode=%s size=%u"), DbgGetMuleClientTCPOpcode(opcode), size);
else
str.Format(_T("protocol=0x%02x opcode=0x%02x size=%u"), protocol, opcode, size);
return str;
}
CString DbgGetClientTCPOpcode(UINT protocol, UINT opcode)
{
CString str;
if (protocol == OP_EDONKEYPROT)
str.Format(_T("%s"), DbgGetDonkeyClientTCPOpcode(opcode));
else if (protocol == OP_PACKEDPROT)
str.Format(_T("%s"), DbgGetMuleClientTCPOpcode(opcode));
else if (protocol == OP_EMULEPROT)
str.Format(_T("%s"), DbgGetMuleClientTCPOpcode(opcode));
else
str.Format(_T("protocol=0x%02x opcode=0x%02x"), protocol, opcode);
return str;
}
void DebugRecv(LPCSTR pszMsg, const CUpDownClient* client, const char* packet, uint32 nIP)
{
// 111.222.333.444 = 15 chars
if (client){
if (client != NULL && packet != NULL)
Debug(_T("%-24hs from %s; %s\n"), pszMsg, client->DbgGetClientInfo(true), DbgGetFileInfo((uchar*)packet));
else if (client != NULL && packet == NULL)
Debug(_T("%-24hs from %s\n"), pszMsg, client->DbgGetClientInfo(true));
else if (client == NULL && packet != NULL)
Debug(_T("%-24hs; %s\n"), pszMsg, DbgGetFileInfo((uchar*)packet));
else
Debug(_T("%-24hs\n"), pszMsg);
}
else{
if (nIP != 0 && packet != NULL)
Debug(_T("%-24hs from %-15s; %s\n"), pszMsg, ipstr(nIP), DbgGetFileInfo((uchar*)packet));
else if (nIP != 0 && packet == NULL)
Debug(_T("%-24hs from %-15s\n"), pszMsg, ipstr(nIP));
else if (nIP == 0 && packet != NULL)
Debug(_T("%-24hs; %s\n"), pszMsg, DbgGetFileInfo((uchar*)packet));
else
Debug(_T("%-24hs\n"), pszMsg);
}
}
void DebugSend(LPCSTR pszMsg, const CUpDownClient* client, const char* packet)
{
if (client != NULL && packet != NULL)
Debug(_T(">>> %-20hs to %s; %s\n"), pszMsg, client->DbgGetClientInfo(true), DbgGetFileInfo((uchar*)packet));
else if (client != NULL && packet == NULL)
Debug(_T(">>> %-20hs to %s\n"), pszMsg, client->DbgGetClientInfo(true));
else if (client == NULL && packet != NULL)
Debug(_T(">>> %-20hs; %s\n"), pszMsg, DbgGetFileInfo((uchar*)packet));
else
Debug(_T(">>> %-20hs\n"), pszMsg);
}
void DebugSend(LPCSTR pszOpcode, uint32 ip, uint16 port)
{
TCHAR szIPPort[22];
_stprintf(szIPPort, _T("%s:%u"), ipstr(ntohl(ip)), port);
Debug(_T(">>> %-20hs to %-21s\n"), pszOpcode, szIPPort);
}
void DebugSendF(LPCSTR pszOpcode, uint32 ip, uint16 port, LPCTSTR pszMsg, ...)
{
va_list args;
va_start(args, pszMsg);
TCHAR szIPPort[22];
_stprintf(szIPPort, _T("%s:%u"), ipstr(ntohl(ip)), port);
CString str;
str.Format(_T(">>> %-20hs to %-21s; "), pszOpcode, szIPPort);
str.AppendFormatV(pszMsg, args);
va_end(args);
Debug(_T("%s\n"), str);
}
void DebugRecv(LPCSTR pszOpcode, uint32 ip, uint16 port)
{
TCHAR szIPPort[22];
_stprintf(szIPPort, _T("%s:%u"), ipstr(ntohl(ip)), port);
Debug(_T("%-24hs from %-21s\n"), pszOpcode, szIPPort);
}
void DebugHttpHeaders(const CStringAArray& astrHeaders)
{
for (int i = 0; i < astrHeaders.GetCount(); i++)
{
const CStringA& rstrHdr = astrHeaders.GetAt(i);
Debug(_T("<%hs\n"), rstrHdr);
}
}
ULONGLONG GetDiskFileSize(LPCTSTR pszFilePath)
{
static BOOL _bInitialized = FALSE;
static DWORD (WINAPI *_pfnGetCompressedFileSize)(LPCTSTR, LPDWORD) = NULL;
if (!_bInitialized){
_bInitialized = TRUE;
(FARPROC&)_pfnGetCompressedFileSize = GetProcAddress(GetModuleHandle(_T("kernel32.dll")), _TWINAPI("GetCompressedFileSize"));
}
// If the file is not compressed nor sparse, 'GetCompressedFileSize' returns the 'normal' file size.
if (_pfnGetCompressedFileSize)
{
ULONGLONG ullCompFileSize;
LPDWORD pdwCompFileSize = (LPDWORD)&ullCompFileSize;
pdwCompFileSize[0] = (*_pfnGetCompressedFileSize)(pszFilePath, &pdwCompFileSize[1]);
if (pdwCompFileSize[0] != INVALID_FILE_SIZE || GetLastError() == NO_ERROR)
return ullCompFileSize;
}
// If 'GetCompressedFileSize' failed or is not available, use the default function
WIN32_FIND_DATA fd;
HANDLE hFind = FindFirstFile(pszFilePath, &fd);
if (hFind == INVALID_HANDLE_VALUE)
return 0;
FindClose(hFind);
return (ULONGLONG)fd.nFileSizeHigh << 32 | (ULONGLONG)fd.nFileSizeLow;
}
// Listview helper function
void GetPopupMenuPos(CListCtrl& lv, CPoint& point)
{
// If the context menu was not opened using the right mouse button,
// but the keyboard (Shift+F10), get a useful position for the context menu.
if (point.x == -1 && point.y == -1)
{
int iIdxItem = lv.GetNextItem(-1, LVNI_SELECTED | LVNI_FOCUSED);
if (iIdxItem != -1)
{
CRect rc;
if (lv.GetItemRect(iIdxItem, &rc, LVIR_BOUNDS))
{
point.x = rc.left + lv.GetColumnWidth(0) / 2;
point.y = rc.top + rc.Height() / 2;
lv.ClientToScreen(&point);
}
}
else
{
point.x = 16;
point.y = 32;
lv.ClientToScreen(&point);
}
}
}
void GetPopupMenuPos(CTreeCtrl& tv, CPoint& point)
{
// If the context menu was not opened using the right mouse button,
// but the keyboard (Shift+F10), get a useful position for the context menu.
if (point.x == -1 && point.y == -1)
{
HTREEITEM hSel = tv.GetNextItem(TVI_ROOT, TVGN_CARET);
if (hSel)
{
CRect rcItem;
if (tv.GetItemRect(hSel, &rcItem, TRUE))
{
point.x = rcItem.left;
point.y = rcItem.top;
tv.ClientToScreen(&point);
}
}
else
{
point.x = 16;
point.y = 32;
tv.ClientToScreen(&point);
}
}
}
time_t safe_mktime(struct tm* ptm)
{
if (ptm == NULL)
return -1;
return mktime(ptm);
}
CString StripInvalidFilenameChars(const CString& strText, bool bKeepSpaces)
{
LPCTSTR pszSource = strText;
CString strDest;
while (*pszSource != _T('\0'))
{
if (!(((_TUCHAR)*pszSource >= 0 && (_TUCHAR)*pszSource <= 31) ||
// lots of invalid chars for filenames in windows :=)
*pszSource == _T('\"') || *pszSource == _T('*') || *pszSource == _T('<') || *pszSource == _T('>') ||
*pszSource == _T('?') || *pszSource == _T('|') || *pszSource == _T('\\') || *pszSource == _T('/') ||
*pszSource == _T(':')) )
{
if (!bKeepSpaces && *pszSource == _T(' '))
strDest += _T("%20");
else
strDest += *pszSource;
}
pszSource++;
}
return strDest;
}
CString CreateED2kLink(const CAbstractFile* pFile, bool bEscapeLink)
{
CString strLink;
strLink.Format(_T("ed2k://|file|%s|%u|%s|"),
EncodeUrlUtf8(StripInvalidFilenameChars(pFile->GetFileName(), false)),
pFile->GetFileSize(),
EncodeBase16(pFile->GetFileHash(),16));
if (bEscapeLink)
strLink += _T("/");
return strLink;
}
CString CreateHTMLED2kLink(const CAbstractFile* f)
{
CString strCode = _T("<a href=\"") + CreateED2kLink(f) + _T("\">") + StripInvalidFilenameChars(f->GetFileName(), true) + _T("</a>");
return strCode;
}
bool operator==(const CCKey& k1,const CCKey& k2)
{
return !md4cmp(k1.m_key, k2.m_key);
}
bool operator==(const CSKey& k1,const CSKey& k2)
{
return !md4cmp(k1.m_key, k2.m_key);
}
CString ipstr(uint32 nIP)
{
// following gives the same string as 'inet_ntoa(*(in_addr*)&nIP)' but is not restricted to ASCII strings
const BYTE* pucIP = (BYTE*)&nIP;
CString strIP;
strIP.ReleaseBuffer(_stprintf(strIP.GetBuffer(3+1+3+1+3+1+3), _T("%u.%u.%u.%u"), pucIP[0], pucIP[1], pucIP[2], pucIP[3]));
return strIP;
}
CStringA ipstrA(uint32 nIP)
{
// following gives the same string as 'inet_ntoa(*(in_addr*)&nIP)' but is not restricted to ASCII strings
const BYTE* pucIP = (BYTE*)&nIP;
CStringA strIP;
strIP.ReleaseBuffer(sprintf(strIP.GetBuffer(3+1+3+1+3+1+3), "%u.%u.%u.%u", pucIP[0], pucIP[1], pucIP[2], pucIP[3]));
return strIP;
}
bool IsDaylightSavingTimeActive(LONG& rlDaylightBias)
{
TIME_ZONE_INFORMATION tzi;
if (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_DAYLIGHT)
return false;
rlDaylightBias = tzi.DaylightBias;
return true;
}
bool IsNTFSVolume(LPCTSTR pszVolume)
{
DWORD dwMaximumComponentLength = 0;
DWORD dwFileSystemFlags = 0;
TCHAR szFileSystemNameBuffer[128];
if (!GetVolumeInformation(pszVolume, NULL, 0, NULL, &dwMaximumComponentLength, &dwFileSystemFlags, szFileSystemNameBuffer, 128))
return false;
return (_tcscmp(szFileSystemNameBuffer, _T("NTFS")) == 0);
}
bool Is
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -