1
0

Begin move to CMake

This commit is contained in:
Scott E. Graves
2017-03-15 12:22:19 -05:00
parent 2da20611cb
commit 28674d716a
5 changed files with 13758 additions and 0 deletions

View File

@@ -0,0 +1,126 @@
#include <siacommon.h>
#include <ttmath/ttmath.h>
#ifdef _WIN32
#include <Wincrypt.h>
#endif
NS_BEGIN(Sia)
NS_BEGIN(Api)
SString GenerateSha256(const SString& str)
{
#ifdef _WIN32
SString ret;
HCRYPTPROV hCryptProv = 0;
HCRYPTHASH hHash = 0;
BOOL ok = CryptAcquireContext(&hCryptProv, nullptr, nullptr, PROV_RSA_AES, 0);
ok = ok && CryptCreateHash(hCryptProv, CALG_SHA_256, 0, 0, &hHash);
ok = ok && CryptHashData(hHash, reinterpret_cast<const BYTE*>(&str[0]), str.ByteLength(), 0);
if (ok)
{
DWORD dwHashLen;
DWORD dwCount = sizeof(DWORD);
if (CryptGetHashParam(hHash, HP_HASHSIZE, reinterpret_cast<BYTE *>(&dwHashLen), &dwCount, 0))
{
std::vector<unsigned char> hash(dwHashLen);
if (CryptGetHashParam(hHash, HP_HASHVAL, reinterpret_cast<BYTE *>(&hash[0]), &dwHashLen, 0))
{
std::ostringstream ss;
ss << std::hex << std::uppercase << std::setfill('0');
for (int c : hash)
{
ss << std::setw(2) << c;
}
ret = ss.str();
}
}
}
if (hHash) CryptDestroyHash(hHash);
if (hCryptProv) CryptReleaseContext(hCryptProv, 0);
return ret;
#else
#endif
}
BOOL RecurDeleteFilesByExtentsion(const SString& folder, const SString& extensionWithDot)
{
#ifdef _WIN32
BOOL ret = FALSE;
WIN32_FIND_DATA fd = { 0 };
SString search;
search.Resize(MAX_PATH + 1);
::PathCombine(&search[0], folder.str().c_str(), L"*.*");
HANDLE find = ::FindFirstFile(search.str().c_str(), &fd);
if (find != INVALID_HANDLE_VALUE)
{
ret = TRUE;
do
{
if (fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if ((SString(fd.cFileName) != ".") && (SString(fd.cFileName) != ".."))
{
SString nextFolder;
nextFolder.Resize(MAX_PATH + 1);
::PathCombine(&nextFolder[0], folder.str().c_str(), fd.cFileName);
ret = RecurDeleteFilesByExtentsion(nextFolder, extensionWithDot);
}
}
else
{
SString ext = ::PathFindExtension(fd.cFileName);
if (ext == extensionWithDot)
{
SString filePath;
filePath.Resize(MAX_PATH + 1);
::PathCombine(&filePath[0], folder.str().c_str(), fd.cFileName);
ret = RetryDeleteFileIfExists(filePath.str().c_str());
}
}
} while (ret && (::FindNextFile(find, &fd) != 0));
}
return ret;
#else
#endif
}
BOOL RetryDeleteFileIfExists(const SString& filePath)
{
#ifdef _WIN32
BOOL ret = TRUE;
if (::PathFileExists(filePath.str().c_str()))
{
ret = RetryableAction(::DeleteFile(filePath.str().c_str()), DEFAULT_RETRY_COUNT, DEFAULT_RETRY_DELAY_MS);
}
return ret;
#else
#endif
}
BOOL RetryAction(std::function<BOOL()> func, std::uint16_t retryCount, const DWORD& retryDelay)
{
#ifdef _WIN32
BOOL ret = FALSE;
while (retryCount-- && !((ret = func())))
{
::Sleep(retryDelay);
}
return ret;
#else
#endif
}
NS_END(2)