📄 otherfunctions.cpp
字号:
if(lResult != ERROR_SUCCESS || lResult < 1)
return 100; //the default for 95 is 100
return dwValue;
} else { //98 or ME
HKEY hKey;
TCHAR szValue[32];
DWORD dwLength = sizeof(szValue);
LONG lResult;
RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("System\\CurrentControlSet\\Services\\VxD\\MSTCP"),
0, KEY_QUERY_VALUE, &hKey);
lResult = RegQueryValueEx(hKey, TEXT("MaxConnections"), NULL, NULL,
(LPBYTE)szValue, &dwLength);
RegCloseKey(hKey);
LONG lMaxConnections;
if(lResult != ERROR_SUCCESS || (lMaxConnections = _tstoi(szValue)) < 1)
return 100; //the default for 98/ME is 100
return lMaxConnections;
}
}
return -1; //give the user the benefit of the doubt, most use NT+ anyway
}
WORD DetectWinVersion()
{
OSVERSIONINFOEX osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
if(!GetVersionEx((OSVERSIONINFO*)&osvi))
{
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
if(!GetVersionEx((OSVERSIONINFO*)&osvi))
return FALSE;
}
switch(osvi.dwPlatformId)
{
case VER_PLATFORM_WIN32_NT:
if(osvi.dwMajorVersion <= 4)
return _WINVER_NT4_;
if(osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
return _WINVER_2K_;
if(osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
return _WINVER_XP_;
return _WINVER_XP_; // never return Win95 if we get the info about a NT system
case VER_PLATFORM_WIN32_WINDOWS:
if(osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0)
return _WINVER_95_;
if(osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10)
return _WINVER_98_;
if(osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90)
return _WINVER_ME_;
break;
default:
break;
}
return _WINVER_95_; // there should'nt be anything lower than this
}
uint64 GetFreeDiskSpaceX(LPCTSTR pDirectory)
{
extern bool g_bUnicoWS;
static BOOL _bInitialized = FALSE;
static BOOL (WINAPI *_pfnGetDiskFreeSpaceEx)(LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER) = NULL;
static BOOL (WINAPI *_pfnGetDiskFreeSpaceExA)(LPCSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER) = NULL;
if (!_bInitialized)
{
_bInitialized = TRUE;
if (g_bUnicoWS)
{
(FARPROC&)_pfnGetDiskFreeSpaceExA = GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "GetDiskFreeSpaceExA");
}
else
{
// Why does it not work to load "GetDiskFreeSpaceExW" explicitly from UnicoWS.dll ?
//extern HMODULE g_hUnicoWS;
//(FARPROC&)_pGetDiskFreeSpaceEx = GetProcAddress(g_hUnicoWS != NULL ? g_hUnicoWS : GetModuleHandle(_T("kernel32.dll")), _TWINAPI("GetDiskFreeSpaceEx"));
(FARPROC&)_pfnGetDiskFreeSpaceEx = GetProcAddress(GetModuleHandle(_T("kernel32.dll")), _TWINAPI("GetDiskFreeSpaceEx"));
}
}
if (_pfnGetDiskFreeSpaceEx)
{
ULARGE_INTEGER nFreeDiskSpace;
ULARGE_INTEGER dummy;
(*_pfnGetDiskFreeSpaceEx)(pDirectory, &nFreeDiskSpace, &dummy, &dummy);
return nFreeDiskSpace.QuadPart;
}
else if (_pfnGetDiskFreeSpaceExA)
{
USES_CONVERSION;
ULARGE_INTEGER nFreeDiskSpace;
ULARGE_INTEGER dummy;
(*_pfnGetDiskFreeSpaceExA)(T2CA(pDirectory), &nFreeDiskSpace, &dummy, &dummy);
return nFreeDiskSpace.QuadPart;
}
else
{
TCHAR cDrive[16];
TCHAR *p = _tcschr(pDirectory, _T('\\'));
if (p)
{
memcpy(cDrive, pDirectory, (p - pDirectory)*sizeof(TCHAR));
cDrive[p - pDirectory] = _T('\0');
}
else
_tcscpy(cDrive, pDirectory);
DWORD dwSectPerClust, dwBytesPerSect, dwFreeClusters, dwDummy;
GetDiskFreeSpace(cDrive, &dwSectPerClust, &dwBytesPerSect, &dwFreeClusters, &dwDummy);
return (dwFreeClusters * dwSectPerClust * dwBytesPerSect);
}
}
CString GetRateString(uint16 rate)
{
switch (rate){
case 0:
return GetResString(IDS_CMT_NOTRATED);
case 1:
return GetResString(IDS_CMT_FAKE);
case 2:
return GetResString(IDS_CMT_POOR);
case 3:
return GetResString(IDS_CMT_GOOD);
case 4:
return GetResString(IDS_CMT_FAIR);
case 5:
return GetResString(IDS_CMT_EXCELLENT);
}
return GetResString(IDS_CMT_NOTRATED);
}
// Returns a BASE32 encoded byte array
//
// [In]
// buffer: Pointer to byte array
// bufLen: Lenght of buffer array
//
// [Return]
// CString object with BASE32 encoded byte array
CString EncodeBase32(const unsigned char* buffer, unsigned int bufLen)
{
CString Base32Buff;
unsigned int i, index;
unsigned char word;
for(i = 0, index = 0; i < bufLen;) {
// Is the current word going to span a byte boundary?
if (index > 3) {
word = (buffer[i] & (0xFF >> index));
index = (index + 5) % 8;
word <<= index;
if (i < bufLen - 1)
word |= buffer[i + 1] >> (8 - index);
i++;
} else {
word = (buffer[i] >> (8 - (index + 5))) & 0x1F;
index = (index + 5) % 8;
if (index == 0)
i++;
}
Base32Buff += (char) base32Chars[word];
}
return Base32Buff;
}
// Returns a BASE16 encoded byte array
//
// [In]
// buffer: Pointer to byte array
// bufLen: Lenght of buffer array
//
// [Return]
// CString object with BASE16 encoded byte array
CString EncodeBase16(const unsigned char* buffer, unsigned int bufLen)
{
CString Base16Buff;
for(unsigned int i = 0; i < bufLen; i++) {
Base16Buff += base16Chars[buffer[i] >> 4];
Base16Buff += base16Chars[buffer[i] & 0xf];
}
return Base16Buff;
}
// Decodes a BASE16 string into a byte array
//
// [In]
// base16Buffer: String containing BASE16
// base16BufLen: Lenght BASE16 coded string's length
//
// [Out]
// buffer: byte array containing decoded string
bool DecodeBase16(const TCHAR *base16Buffer, unsigned int base16BufLen, byte *buffer, unsigned int bufflen)
{
unsigned int uDecodeLengthBase16 = DecodeLengthBase16(base16BufLen);
if (uDecodeLengthBase16 > bufflen)
return false;
memset(buffer, 0, uDecodeLengthBase16);
for(unsigned int i = 0; i < base16BufLen; i++) {
int lookup = toupper(base16Buffer[i]) - '0';
// Check to make sure that the given word falls inside a valid range
byte word = 0;
if ( lookup < 0 || lookup >= BASE16_LOOKUP_MAX)
word = 0xFF;
else
word = base16Lookup[lookup][1];
if(i % 2 == 0) {
buffer[i/2] = word << 4;
} else {
buffer[(i-1)/2] |= word;
}
}
return true;
}
// Calculates length to decode from BASE16
//
// [In]
// base16Length: Actual length of BASE16 string
//
// [Return]
// New length of byte array decoded
unsigned int DecodeLengthBase16(unsigned int base16Length)
{
return base16Length / 2U;
}
uint32 DecodeBase32(LPCTSTR pszInput, uchar* paucOutput, uint32 nBufferLen)
{
if (pszInput == NULL)
return false;
uint32 nDecodeLen = (_tcslen(pszInput)*5)/8;
if ((_tcslen(pszInput)*5) % 8 > 0)
nDecodeLen++;
uint32 nInputLen = _tcslen( pszInput );
if (paucOutput == NULL || nBufferLen == 0)
return nDecodeLen;
if (nDecodeLen > nBufferLen || paucOutput == NULL)
return 0;
DWORD nBits = 0;
int nCount = 0;
for ( int nChars = nInputLen ; nChars-- ; pszInput++ )
{
if ( *pszInput >= 'A' && *pszInput <= 'Z' )
nBits |= ( *pszInput - 'A' );
else if ( *pszInput >= 'a' && *pszInput <= 'z' )
nBits |= ( *pszInput - 'a' );
else if ( *pszInput >= '2' && *pszInput <= '7' )
nBits |= ( *pszInput - '2' + 26 );
else
return 0;
nCount += 5;
if ( nCount >= 8 )
{
*paucOutput++ = (BYTE)( nBits >> ( nCount - 8 ) );
nCount -= 8;
}
nBits <<= 5;
}
return nDecodeLen;
}
uint32 DecodeBase32(LPCTSTR pszInput, CAICHHash& Hash){
return DecodeBase32(pszInput, Hash.GetRawHash(), Hash.GetHashSize());
}
CWebServices::CWebServices()
{
m_tDefServicesFileLastModified = 0;
}
CString CWebServices::GetDefaultServicesFile() const
{
return thePrefs.GetConfigDir() + _T("webservices.dat");
}
void CWebServices::RemoveAllServices()
{
m_aServices.RemoveAll();
m_tDefServicesFileLastModified = 0;
}
int CWebServices::ReadAllServices()
{
RemoveAllServices();
CString strFilePath = GetDefaultServicesFile();
FILE* readFile = _tfsopen(strFilePath, _T("r"), _SH_DENYWR);
if (readFile != NULL)
{
CString name, url, sbuffer;
while (!feof(readFile))
{
TCHAR buffer[1024];
if (_fgetts(buffer, ARRSIZE(buffer), readFile) == NULL)
break;
sbuffer = buffer;
// ignore comments & too short lines
if (sbuffer.GetAt(0) == _T('#') || sbuffer.GetAt(0) == _T('/') || sbuffer.GetLength() < 5)
continue;
int iPos = sbuffer.Find(_T(','));
if (iPos > 0)
{
CString strUrlTemplate = sbuffer.Right(sbuffer.GetLength() - iPos - 1).Trim();
if (!strUrlTemplate.IsEmpty())
{
bool bFileMacros = false;
static const LPCTSTR _apszMacros[] = { _T("#hashid"), _T("#filesize"), _T("#filename") };
for (int i = 0; i < ARRSIZE(_apszMacros); i++)
{
if (strUrlTemplate.Find(_apszMacros[i]) != -1)
{
bFileMacros = true;
break;
}
}
SEd2kLinkService svc;
svc.uMenuID = MP_WEBURL + m_aServices.GetCount();
svc.strMenuLabel = sbuffer.Left(iPos).Trim();
svc.strUrl = strUrlTemplate;
svc.bFileMacros = bFileMacros;
m_aServices.Add(svc);
}
}
}
fclose(readFile);
struct _stat st;
if (_tstat(strFilePath, &st) == 0)
m_tDefServicesFileLastModified = st.st_mtime;
}
return m_aServices.GetCount();
}
int CWebServices::GetAllMenuEntries(CMenu& rMenu, DWORD dwFlags)
{
if (m_aServices.GetCount() == 0)
{
ReadAllServices();
}
else
{
struct _stat st;
if (_tstat(GetDefaultServicesFile(), &st) == 0 && st.st_mtime > m_tDefServicesFileLastModified)
ReadAllServices();
}
int iMenuEntries = 0;
for (int i = 0; i < m_aServices.GetCount(); i++)
{
const SEd2kLinkService& rSvc = m_aServices.GetAt(i);
if ((dwFlags & WEBSVC_GEN_URLS) && rSvc.bFileMacros)
continue;
if ((dwFlags & WEBSVC_FILE_URLS) && !rSvc.bFileMacros)
continue;
if (rMenu.AppendMenu(MF_STRING, MP_WEBURL + i, rSvc.strMenuLabel))
iMenuEntries++;
}
return iMenuEntries;
}
bool CWebServices::RunURL(const CAbstractFile* file, UINT uMenuID)
{
for (int i = 0; i < m_aServices.GetCount(); i++)
{
const SEd2kLinkService& rSvc = m_aServices.GetAt(i);
if (rSvc.uMenuID == uMenuID)
{
CString strUrlTemplate = rSvc.strUrl;
if (file != NULL)
{
// Convert hash to hexadecimal text and add it to the URL
strUrlTemplate.Replace(_T("#hashid"), md4str(file->GetFileHash()));
// Add file size to the URL
CString temp;
temp.Format(_T("%u"), file->GetFileSize());
strUrlTemplate.Replace(_T("#filesize"), temp);
// add filename to the url
strUrlTemplate.Replace(_T("#filename"), URLEncode(file->GetFileName()));
}
// Open URL
TRACE("Starting URL: %s\n", strUrlTemplate);
return (int)ShellExecute(NULL, NULL, strUrlTemplate, NULL, thePrefs.GetAppDir(), SW_SHOWDEFAULT) > 32;
}
}
return false;
}
void CWebServices::Edit()
{
ShellExecute(NULL, _T("open"), thePrefs.GetTxtEditor(), _T("\"") + thePrefs.GetConfigDir() + _T("webservices.dat\""), NULL, SW_SHOW);
}
typedef struct
{
LPCTSTR pszInitialDir;
LPCTSTR pszDlgTitle;
} BROWSEINIT, *LPBROWSEINIT;
extern "C" int CALLBACK BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM /*lParam*/, LPARAM lpData)
{
if (uMsg == BFFM_INITIALIZED)
{
// Set initial directory
if ( ((LPBROWSEINIT)lpData)->pszInitialDir != NULL )
SendMessage(hWnd, BFFM_SETSELECTION, TRUE, (LPARAM)((LPBROWSEINIT)lpData)->pszInitialDir);
// Set dialog's window title
if ( ((LPBROWSEINIT)lpData)->pszDlgTitle != NULL )
SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)((LPBROWSEINIT)lpData)->pszDlgTitle);
}
return 0;
}
bool SelectDir(HWND hWnd, LPTSTR pszPath, LPCTSTR pszTitle, LPCTSTR pszDlgTitle)
{
BOOL bResult = FALSE;
CoInitialize(0);
LPMALLOC pShlMalloc;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -